linchaocmcc 发表于 2018-4-17 23:03:45

python中为什么可以这么进行变量赋值

list1是个含4个元素的列表,为什么下列表达式成立? 等号右边不是元组吗

(a, b, c, d) = list1

BngThea 发表于 2018-4-18 07:38:14

可以一一对应进行赋值,赋值完了再将这些变量组成一个元祖

aqqq 发表于 2018-4-18 09:07:38

>>> (a,b,c,d) = list1
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
    (a,b,c,d) = list1
NameError: name 'list1' is not defined

thexiosi 发表于 2018-4-18 18:23:13

本帖最后由 thexiosi 于 2018-4-18 18:26 编辑

hi

完成四个变量的一一对应赋值,最终形成一个元祖,示例如下>>> list1 =
>>> (a,b,c,d) = list1
>>> print(a,b,c,d)
1 2 3 4
>>> type((a,b,c,d))
<class 'tuple'>
页: [1]
查看完整版本: python中为什么可以这么进行变量赋值