内部列表或整数的串联 [英] Concatenation of inner lists or ints
问题描述
我觉得我缺少明显的东西,但是那是...我想从这里走
I feel like I'm missing something obvious, but there it is... I would like to go from:
lst = [[0, 1, 3, 7, 8, 11, 12], [8, 0, 1, 2, 3, 14], 2]
收件人:
output = [0, 1, 3, 7, 8, 11, 12, 8, 0, 1, 2, 3, 14, 2]
我可以使用for循环来做到这一点,例如:
I can do this with a for loop such as:
output = []
for l in lst:
if hasattr(l, '__iter__'):
output.extend(l)
else:
output.append(l)
也许for循环很好,但是感觉应该有一种更优雅的方法来实现...用numpy做到这一点似乎更加费解,因为参差不齐的数组不易处理...所以您不能(例如):
Maybe the for-loop is fine, but it feels like there should be a more elegant way to do this... Trying to do this with numpy seems even more convoluted because ragged arrays aren't easily handled... so you can't (for example):
output = np.asanyarray(lst).flatten().tolist()
提前谢谢.
更新:
这是我在@ T.J和@Ashwini提供的两种方法之间的比较-多亏了两者!
Here's my comparison between the two methods provided by @T.J and @Ashwini - thanks to both!
In [5]: %paste
from itertools import chain
from collections import Iterable
lis = [[0, 1, 3, 7, 8, 11, 12], [8, 0, 1, 2, 3, 14], 2]
def solve(lis):
for x in lis:
if isinstance(x, Iterable) and not isinstance(x, basestring):
yield x
else:
yield [x]
%timeit list(chain.from_iterable(solve(lis)))
%timeit [a for x in lis for a in (x if isinstance(x, Iterable) and not isinstance(x,basestring) else [x])]
## -- End pasted text --
100000 loops, best of 3: 10.1 us per loop
100000 loops, best of 3: 8.12 us per loop
Update2:
...
lis = lis *10**5
%timeit list(chain.from_iterable(solve(lis)))
%timeit [a for x in lis for a in (x if isinstance(x, Iterable) and not isinstance(x,basestring) else [x])]
## -- End pasted text --
1 loops, best of 3: 699 ms per loop
1 loops, best of 3: 698 ms per loop
推荐答案
这是一种使用列表推导的非常简单的方法:
Here is a pretty straightforward approach that uses a list comprehension:
>>> data = [[0, 1, 3, 7, 8, 11, 12], [8, 0, 1, 2, 3, 14], 2]
>>> [a for x in data for a in (x if isinstance(x, list) else [x])]
[0, 1, 3, 7, 8, 11, 12, 8, 0, 1, 2, 3, 14, 2]
这里是时序比较,看来我的版本稍快(请注意,我还修改了代码以使用collections.Iterable
以确保比较公平):
Here are timing comparisons, it looks like my version is slightly faster (note that I modified my code to use collections.Iterable
as well to make sure the comparison is fair):
In [9]: %timeit list(chain.from_iterable(solve(data)))
100000 loops, best of 3: 9.22 us per loop
In [10]: %timeit [a for x in data for a in (x if isinstance(x, Iterable) else [x])]
100000 loops, best of 3: 6.45 us per loop
这篇关于内部列表或整数的串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!