如何使用2个列表打印循环?

时间:2011-02-04 17:50:39

标签: python list for-loop

我有2个名单:

filtered_items_list = [src1, scr2, scr3]
filtered_items_url_list = [url1, url2, url3]

我想将其打印为

src1, url1
src2, url2
src3, url3

如果我尝试:

>>> for src, url in filtered_items_list, filtered_items_url_list:
    print src, url

我收到此错误:

ValueError: too many values to unpack

如果我试试这个:

>>> for src in filtered_items_list:
        for url in filtered_items_url_list:
            print """"src: %s; url: %s""" % (src, url)

我明白了:

"src: src1; url: url1
"src: src1; url: url2
"src: src1; url: url3
"src: scr2; url: url1
"src: scr2; url: url2
"src: scr2; url: url3
"src: scr3; url: url1
"src: scr3; url: url2
"src: scr3; url: url3
>>> 

你能建议一个解决方案吗?谢谢!

1 个答案:

答案 0 :(得分:9)

您应该使用zip()

for src, url in zip(filtered_items_list, filtered_items_url_list):
    print src, url