简体   繁体   中英

How to iterate over two lists?

I am 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.

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
>>> 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM