如何迭代两个列表 - python [英] How to iterate over two lists - python

查看:129
本文介绍了如何迭代两个列表 - python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们。我试图在pyGTk中创建一个HBox列表:

  self.keyvalueboxes = [] 
。在范围KEYVAL(1,self.keyvaluelen):
self.keyvalueboxes.append(gtk.HBox(假,5))

但是我想要在列表中运行并分配一个文本输入&一个标签到每一个都存储在列表中。



对不起,我不是非常具体,但如果你需要更多的帮助, 'm我会帮忙的!



谢谢!

解决方案

如果您的列表长度相同,请使用zip

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


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!

解决方案

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

这篇关于如何迭代两个列表 - python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆