亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

python for循環(huán)問題,很困擾
黃舟
黃舟 2017-04-18 10:31:51
0
2
575
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)心的淡定與從容!

reply all(2)
PHPzhong

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 = 1There 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//不確定
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template