简体   繁体   English

一次从每个列表中返回 1 项

[英]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.

相关问题 我的迭代只会返回我列表中的最后一项,而不是一次返回每个项目以传递给另一个函数以在主列表中使用 - My iteration will only return the last item in my list, not return each item one at a time to pass to another function for use in a master list 迭代一个列表,为每个项目分配一个变量并返回它 - Iterate a list, assign a variable to each item and return it 查找列表中的每个项目到 List2 中的项目。 如果有匹配返回这样的值,如果没有则删除整行 - Lookup Each item from a list to items from List2. If there's a match return such value, if not delete the entire row 需要将第一个列表中的项目与第二个列表中的每个其他项目进行比较,并为每次签入新列表返回通过或失败 - need to compare item from first list to every other item in second list and return pass or fail for each check in new list 对于 mongo 集合中的每个 from 列表插入项 - For each from list insert item in mongo collection 将一个列表中的每个项目添加到另一个列表中的每个项目到第三个列表 - Adding each item from one list to each item from another list to third list 将列表中的每个项目与一个值进行比较,并在Python中返回T / F - Compare each item in list with a value and return T/F in Python Jinja2循环通过python列表以返回xml中的每个项目 - Jinja2 looping through a python list to return each item in xml 如何在Python中从列表中返回匹配项 - How to return a matching item from a list in Python 如何从 KivyMD 列表中返回所选项目? - How to return selected item from KivyMD list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM