在 Python 中使用列表理解来执行类似于 zip() 的操作?

发布于 2024-08-20 01:59:54 字数 440 浏览 5 评论 0原文

我是一名 Python 新手,我想做的一件事就是围绕列表理解进行思考。我可以看到这是一个非常强大的功能,值得学习。

cities = ['Chicago', 'Detroit', 'Atlanta']
airports = ['ORD', 'DTW', 'ATL']

print zip(cities,airports)
[('Chicago', 'ORD'), ('Detroit', 'DTW'), ('Atlanta', 'ATL')]

如何使用列表理解,以便可以将结果作为列表中的一系列列表而不是列表中的一系列元组来获取?

[['Chicago', 'ORD'], ['Detroit', 'DTW'], ['Atlanta', 'ATL']]

(我意识到字典在这种情况下可能更合适,但我只是想更好地理解列表)。谢谢!

I'm a Python newbie and one of the things I am trying to do is wrap my head around list comprehension. I can see that it's a pretty powerful feature that's worth learning.

cities = ['Chicago', 'Detroit', 'Atlanta']
airports = ['ORD', 'DTW', 'ATL']

print zip(cities,airports)
[('Chicago', 'ORD'), ('Detroit', 'DTW'), ('Atlanta', 'ATL')]

How do I use list comprehension so I can get the results as a series of lists within a list, rather than a series of tuples within a list?

[['Chicago', 'ORD'], ['Detroit', 'DTW'], ['Atlanta', 'ATL']]

(I realize that dictionaries would probably be more appropriate in this situation, but I'm just trying to understand lists a bit better). Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

扬花落满肩 2024-08-27 01:59:54

像这样的东西:

[[c, a] for c, a in zip(cities, airports)]

或者,list构造函数可以将元组转换为列表:

[list(x) for x in zip(cities, airports)]

或者,在这种情况下,map函数稍微不那么冗长:

map(list, zip(cities, airports))

Something like this:

[[c, a] for c, a in zip(cities, airports)]

Alternately, the list constructor can convert tuples to lists:

[list(x) for x in zip(cities, airports)]

Or, the map function is slightly less verbose in this case:

map(list, zip(cities, airports))
走野 2024-08-27 01:59:54

如果您想完全不使用 zip 来完成此操作,则必须执行以下操作:

[ [cities[i],airports[i]] for i in xrange(min(len(cities), len(airports))) ]

但除了智力练习之外,没有理由这样做。

使用 map(list, zip(cities, airports)) 更短、更简单,而且几乎肯定会运行得更快。

If you wanted to do it without using zip at all, you would have to do something like this:

[ [cities[i],airports[i]] for i in xrange(min(len(cities), len(airports))) ]

but there is no reason to do that other than an intellectual exercise.

Using map(list, zip(cities, airports)) is shorter, simpler and will almost certainly run faster.

尾戒 2024-08-27 01:59:54

如果没有 zipmapitertools 的帮助,列表理解无法在多个序列上建立“并行循环”——只能是简单的在一个序列上循环,或在多个序列上“嵌套”循环。

A list comprehension, without some help from zip, map, or itertools, cannot institute a "parallel loop" on multiple sequences -- only simple loops on one sequence, or "nested" loops on multiple ones.

寂寞清仓 2024-08-27 01:59:54

这需要 zip 的输出并将所有元组转换为列表:

map(list, zip(cities, airports))

至于每个元组的性能:

$ python -m timeit -c '[ [a, b] for a, b in zip(xrange(100), xrange(100)) ]'
10000 loops, best of 3: 68.3 usec per loop

$ python -m timeit -c 'map(list, zip(xrange(100), xrange(100)))'
10000 loops, best of 3: 75.4 usec per loop

$ python -m timeit -c '[ list(x) for x in zip(range(100), range(100)) ]'
10000 loops, best of 3: 99.9 usec per loop

This takes zip's output and converts all tuples to lists:

map(list, zip(cities, airports))

As for the performance of each:

$ python -m timeit -c '[ [a, b] for a, b in zip(xrange(100), xrange(100)) ]'
10000 loops, best of 3: 68.3 usec per loop

$ python -m timeit -c 'map(list, zip(xrange(100), xrange(100)))'
10000 loops, best of 3: 75.4 usec per loop

$ python -m timeit -c '[ list(x) for x in zip(range(100), range(100)) ]'
10000 loops, best of 3: 99.9 usec per loop
神仙妹妹 2024-08-27 01:59:54

也可以使用enumerate

[[y,airports[x]] for x,y in enumerate(cities)]

Possible to use enumerate, as well:

[[y,airports[x]] for x,y in enumerate(cities)]
妄司 2024-08-27 01:59:54

如果您想使用列表理解,并创建一个变量作为字典,这里是一个不错的方法:

newvariable = {key: value for key, value in zip(cities,airports)}

If you wanted to use list comprehension, and create a variable as a dictionary here is a decent way to do it:

newvariable = {key: value for key, value in zip(cities,airports)}
小…红帽 2024-08-27 01:59:54

您可以使用列表理解来组合两个列表,但在现实世界中,您应该更喜欢内置函数 (zip()) 而不是复杂的流语句。

zip_alternative=[ (cities[x],airports[y]) for x  in range(len(cities)) for y  in range(len(airports)) if x==y ]

You can use list comprehension to combine two lists,however in the real world you should prefer builtin functions (zip()) to complex flow statements.

zip_alternative=[ (cities[x],airports[y]) for x  in range(len(cities)) for y  in range(len(airports)) if x==y ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文