a=['one','two']
for p,q in a:
print p,q
出錯
a=[['one','tow']]
for p,q in a:
print p,q
這樣就可以
第一種我一直就認為是這樣的:a的one,two分別賦值給p,q。
不然得怎么理解?
人生最曼妙的風景,竟是內(nèi)心的淡定與從容!
In Python, a for loop runs through each element in a list (or generator). So the correct form of the first loop is:
for x in a:
print(x)
In addition, in Python we can write:
x, y = (1, 2)
From now on, x is 1 and y is 2. We can also write:
x, y = [1, 2]
Has the same effect.
If we write: x, y = 1
There is an error, except for the exact same error in one of your for loops.
In the second f example the list [['one', 'two']]
只有個元素,就是['one', 'two']
。for循環(huán)要跑一次,這一次p, q
就是['one', 'two']
is similar to the example above.
In the first example, a has two elements, namely 'one', 'two'. The assignment is uncertain on p and q.
In the second example, a has an element ['one', 'two' '], the order is determined.
p, q = [x, y] //可行
p, q = x, y//不確定