[英]Return 1 item from each list at a time
I have two lists:我有两个清单:
chars = 'abcdefghijklmnopqrstuvwxyz'
columns = ['item', 'desc', 'price', 'currency', 'country', 'state', 'postcode']
Basically, I want it to print:基本上,我希望它打印:
a item
b desc
c price
etc...
I tried doing:我试着做:
for i in range(0, len(columns)):
for char in chars:
print(char, columns[i])
But that prints each letter with column[i]但这会用 column[i] 打印每个字母
You can use zip
for this:您可以为此使用
zip
:
for char, col in zip(chars, columns):
print(char, col)
Use zip(), iterate then print.使用 zip(),迭代然后打印。
zipped = zip(chars, columns)
for item in zipped:
print(item[0], item[1])
Just a way without an explicit alphabet (which is always error-prone and I've seen many people forget letters):只是一种没有明确字母表的方法(这总是容易出错,我见过很多人忘记字母):
for i, col in enumerate(columns, start=ord('a')):
print(chr(i), col)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.