我在Python中有两个迭代,我想成对地遍历它们:
foo = (1, 2, 3)
bar = (4, 5, 6)
for (f, b) in some_iterator(foo, bar):
print "f: ", f, "; b: ", b
Run Code Online (Sandbox Code Playgroud)
它应该导致:
f: 1; b: 4
f: 2; b: 5
f: 3; b: 6
Run Code Online (Sandbox Code Playgroud)
一种方法是迭代索引:
for i in xrange(len(foo)):
print "f: ", foo[i], "; b: ", b[i]
Run Code Online (Sandbox Code Playgroud)
但这对我来说似乎有点不合时宜.有没有更好的方法呢?
我偶然发现了以下代码:
for i,a in enumerate(attributes):
labels.append(Label(root, text = a, justify = LEFT).grid(sticky = W))
e = Entry(root)
e.grid(column=1, row=i)
entries.append(e)
entries[i].insert(INSERT,"text to insert")
Run Code Online (Sandbox Code Playgroud)
我不明白'i,a'位和搜索谷歌有关'for'的信息是一个痛苦的屁股,当我尝试和代码使用时我得到错误:
ValueError:需要多于1个值才能解压缩
有谁知道它做了什么或与它有关,我可以谷歌了解更多?