如何使用列表理解将元组的元组转换为一维列表?
我有一个元组的元组 - 例如:
tupleOfTuples = ((1, 2), (3, 4), (5,))
我想将其转换为按顺序排列的所有元素的平面一维列表:
[1, 2, 3, 4, 5]
我一直在尝试通过列表理解来完成此任务。但我似乎无法弄清楚。我能够通过 for-each 循环来完成它:
myList = []
for tuple in tupleOfTuples:
myList = myList + list(tuple)
但我觉得必须有一种方法可以通过列表理解来做到这一点。
一个简单的 [list(tuple) for tupleOfTuples]
只是为您提供一个列表列表,而不是单个元素。我想我也许可以在此基础上使用解包运算符来解包列表,如下所示:
[*list(tuple) for tuple in tupleOfTuples]
或
[*(list(tuple)) for tuple in tupleOfTuples]
... 但这不起作用。有什么想法吗?或者我应该坚持循环?
I have a tuple of tuples - for example:
tupleOfTuples = ((1, 2), (3, 4), (5,))
I want to convert this into a flat, one-dimensional list of all the elements in order:
[1, 2, 3, 4, 5]
I've been trying to accomplish this with list comprehension. But I can't seem to figure it out. I was able to accomplish it with a for-each loop:
myList = []
for tuple in tupleOfTuples:
myList = myList + list(tuple)
But I feel like there must be a way to do this with a list comprehension.
A simple [list(tuple) for tuple in tupleOfTuples]
just gives you a list of lists, instead of individual elements. I thought I could perhaps build on this by using the unpacking operator to then unpack the list, like so:
[*list(tuple) for tuple in tupleOfTuples]
or
[*(list(tuple)) for tuple in tupleOfTuples]
... but that didn't work. Any ideas? Or should I just stick to the loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它通常被称为展平嵌套结构。
只是为了演示效率:
ETA:请不要使用
tuple
作为变量名,它会隐藏内置变量。it's typically referred to as flattening a nested structure.
Just to demonstrate efficiency:
ETA: Please don't use
tuple
as a variable name, it shadows built-in.如果您没有很多元组,只需使用
sum
即可。如果您确实有很多元组,请使用 列表理解 或
chain.from_iterable
以防止sum
的二次行为。微基准测试:
Python 2.6
短元组的长元组
长元组的短元组
Python 3.1
短元组的长元组
长元组的短元组
观察:
sum
会更快。list(chain.from_iterable(x))
会更快。Just use
sum
if you don't have a lot of tuples.If you do have a lot of tuples, use list comprehension or
chain.from_iterable
to prevent the quadratic behavior ofsum
.Micro-benchmarks:
Python 2.6
Long tuple of short tuples
Short tuple of long tuples
Python 3.1
Long tuple of short tuples
Short tuple of long tuples
Observation:
sum
is faster if the outer tuple is short.list(chain.from_iterable(x))
is faster if the outer tuple is long.您将元组链接在一起:
如果您熟悉
itertools
,那么应该非常容易阅读,并且如果没有显式的list
,您甚至可以得到生成器形式的结果。You're chaining the tuples together:
Should be pretty readable if you're familiar with
itertools
, and without the explicitlist
you even have your result in generator form.我喜欢在这种情况下使用“reduce”(这就是“reduce”的用途!)
I like using 'reduce' in this situation (this is what reduce made for!)
这些答案中的大多数仅适用于单一级别的扁平化。如需更全面的解决方案,请尝试此(来自 http://rightfootin .blogspot.com/2006/09/more-on-python-flatten.html):
Most of these answers will only work for a single level of flattening. For a more comprehensive solution, try this (from http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html):
使用 itertools.chain 的另一个解决方案
Another solution using itertools.chain
对于多级且可读的代码:
我无法将其放在一行中(并且即使到目前为止仍保持可读)
For multilevel, and readable code:
I could not get this to fit in one line (and remain readable, even by far)