gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:11:09 24 4
gpt4 key购买 nike

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

您能提出解决方案吗?谢谢!

最佳答案

你应该使用zip():

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

关于python - 如何使用 2 个列表打印 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4901220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com