How to iterate over two lists - python

喜欢而已 提交于 2019-12-01 07:57:04

问题


Hey guys. I'm trying to do something in pyGTk where I build a list of HBoxes:

self.keyvalueboxes = []
for keyval in range(1,self.keyvaluelen):
    self.keyvalueboxes.append(gtk.HBox(False, 5))

But I then want to run over the list and assign A text entry & a label into each one both of which are stored in a list.

I'm sorry I'm not being very specific, but if you need more help with what I'm doing I'll help!

Thanks!


回答1:


If your list are of equal length use zip

>>> x = ['a', 'b', 'c', 'd']
>>> y = [1, 2, 3, 4]
>>> z = zip(x,y)
>>> z
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
>>> for l in z: print l[0], l[1]
... 
a 1
b 2
c 3
d 4
>>> 



回答2:


Check out http://docs.python.org/library/functions.html#zip. It lets you iterate over two lists at the same time.



来源:https://stackoverflow.com/questions/4038481/how-to-iterate-over-two-lists-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!