Python 内部列表或整数的串联

Python 内部列表或整数的串联,python,numpy,concatenation,flatten,Python,Numpy,Concatenation,Flatten,我觉得我错过了一些明显的东西,但这就是。。。我想从: lst = [[0, 1, 3, 7, 8, 11, 12], [8, 0, 1, 2, 3, 14], 2] 致: 我可以使用for循环来执行此操作,例如: output = [] for l in lst: if hasattr(l, '__iter__'): output.extend(l) else: output.append(l) 也许for循环很好,但感觉应该有一种更优雅的方式

我觉得我错过了一些明显的东西,但这就是。。。我想从:

lst = [[0, 1, 3, 7, 8, 11, 12], [8, 0, 1, 2, 3, 14], 2]
致:

我可以使用for循环来执行此操作,例如:

output = []
for l in lst:
    if hasattr(l, '__iter__'):
        output.extend(l)
    else:
        output.append(l)
也许for循环很好,但感觉应该有一种更优雅的方式来实现这一点。。。试图用numpy实现这一点似乎更加复杂,因为不规则的数组不容易处理。。。所以你不能(例如):

提前谢谢

更新:

...
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
下面是我对@T.J和@Ashwini提供的两种方法的比较——感谢这两种方法

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
更新2:

...
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

您可以像这样使用
itertools.chain

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

>>> list(chain.from_iterable(solve(lis)))
[0, 1, 3, 7, 8, 11, 12, 8, 0, 1, 2, 3, 14, 2]
也适用于字符串:

>>> lis = [[0, 1, 3, 7, 8, 11, 12], [8, 0, 1, 2, 3, 14], "234"]
>>> list(chain.from_iterable(solve(lis)))
[0, 1, 3, 7, 8, 11, 12, 8, 0, 1, 2, 3, 14, '234']
时间比较:

>>> lis = lis *(10**4)
#modified version of FJ's answer that works for strings as well
>>> %timeit [a for x in lis for a in (x if isinstance(x, Iterable) and not isinstance(x,basestring) else [x])]
10 loops, best of 3: 110 ms per loop

>>> %timeit list(chain.from_iterable(solve(lis)))
1 loops, best of 3: 98.3 ms per loop

您可以像这样使用
itertools.chain

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

>>> list(chain.from_iterable(solve(lis)))
[0, 1, 3, 7, 8, 11, 12, 8, 0, 1, 2, 3, 14, 2]
也适用于字符串:

>>> lis = [[0, 1, 3, 7, 8, 11, 12], [8, 0, 1, 2, 3, 14], "234"]
>>> list(chain.from_iterable(solve(lis)))
[0, 1, 3, 7, 8, 11, 12, 8, 0, 1, 2, 3, 14, '234']
时间比较:

>>> lis = lis *(10**4)
#modified version of FJ's answer that works for strings as well
>>> %timeit [a for x in lis for a in (x if isinstance(x, Iterable) and not isinstance(x,basestring) else [x])]
10 loops, best of 3: 110 ms per loop

>>> %timeit list(chain.from_iterable(solve(lis)))
1 loops, best of 3: 98.3 ms per loop

您可以使用生成器使列表仅由iterables组成

((i if isinstance(i,Iterable) else [i]) 
然后使用以下任一方法将它们连接到一个列表中:

您能试试
sum

from collections import Iterable
lis = [[0, 1, 3, 7, 8, 11, 12], [8, 0, 1, 2, 3, 14], 2]
sum(((i if isinstance(i,Iterable) else [i]) for i in lis), [])
您应该为sum
[]
提供初始值,以便从中开始求和

itertools.chain()
但在这种情况下,您应该将上层列表解压为一组列表

import itertools
lis = [[0, 1, 3, 7, 8, 11, 12], [8, 0, 1, 2, 3, 14], 2]
list(itertools.chain(*((i if isinstance(i,Iterable) else [i]) for i in lis)))
或者只是列出理解

[a for x in input for a in (x if isinstance(x, Iterable) else [x])]

但我们应该记住,字符串也是可编辑的。若上层有细绳,它会被吐进木炭里

您可以使用生成器使列表仅由iterables组成

((i if isinstance(i,Iterable) else [i]) 
然后使用以下任一方法将它们连接到一个列表中:

您能试试
sum

from collections import Iterable
lis = [[0, 1, 3, 7, 8, 11, 12], [8, 0, 1, 2, 3, 14], 2]
sum(((i if isinstance(i,Iterable) else [i]) for i in lis), [])
您应该为sum
[]
提供初始值,以便从中开始求和

itertools.chain()
但在这种情况下,您应该将上层列表解压为一组列表

import itertools
lis = [[0, 1, 3, 7, 8, 11, 12], [8, 0, 1, 2, 3, 14], 2]
list(itertools.chain(*((i if isinstance(i,Iterable) else [i]) for i in lis)))
或者只是列出理解

[a for x in input for a in (x if isinstance(x, Iterable) else [x])]

但我们应该记住,字符串也是可编辑的。若上层有细绳,它会被吐进木炭里

这里有一个非常简单的方法,使用列表理解:

>>> 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
,以确保比较公平):


下面是一个使用列表理解的非常简单的方法:

>>> 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
,以确保比较公平):



…或者对于不规则列表,请尝试。是的,当然是-为dup道歉,没有想到搜索“flatten”。:/可能是因为(在Python 2.x上)您可以从compiler.ast import flatte执行
;展平([[0,1,3,7,8,11,12],[8,0,1,2,3,14,2])
->
[0,1,3,7,8,11,12,8,0,1,2,3,14,2]
在计时解决方案之前更改列表大小:
lis=lis*10**5
itertools.chain
对于较大的列表,比正常的列表理解速度更快,对于小列表,计时并不重要。非常好的一点-看起来像
lis=lis*10**5
几乎是这两种方法之间的转折点…或者对于不规则列表,试试看。是的,当然是-为dup道歉,没有想到搜索“展平”。/可能是因为(在Python 2.x上)您只需从compiler.ast import flatte执行
;展平([[0,1,3,7,8,11,12],[8,0,1,2,3,14,2])
->
[0,1,3,7,8,11,12,8,0,1,2,3,14,2]
在计时解决方案之前更改列表大小:
lis=lis*10**5
itertools.chain
对于较大的列表,比正常的列表理解速度更快,对于小列表,时间并不重要。非常好的一点-看起来像
lis=lis*10**5
几乎是这两种方法之间的转折点。只有当
lis
的所有项目都合适时,你的方法才会起作用。你是对的,我应该注意这一点。只有
lis
的所有项目都合适时,你的方法才会起作用太好了。你说得对。我应该处理好这个。谢谢,这是一个很好的解决方案。你知道这是否比@F.J的答案更有效吗?@blazetopher FJ的解决方案会更快,因为他使用了列表理解,我使用了一个函数来提高解决方案的可读性。如果数据中存在字符串,我的解决方案也会处理这种情况。这两种方法都有好处!再次感谢@blazetopher看看我的更新解决方案。有趣的是,你的timeit结果与@T.J的不同,我正在编辑我的帖子以显示我的对比,这是一个很好的解决方案。你知道这是否比@F.J的答案更有效吗?@blazetopher FJ的解决方案会更快,因为他使用了列表理解,我使用了一个函数来提高解决方案的可读性。如果数据中存在字符串,我的解决方案也会处理这种情况。这两种方法都有好处!再次感谢@blazetopher看看我的更新解决方案。有趣的是,你的timeit结果与@T.J的不同,我正在编辑我的帖子,以显示我的对比指数!我自己也非常接近这一点(没有发布),但没有得到正确的逻辑部分。你知道这是否比@Ashwini的回答更有效?@blazetopher刚刚添加了一个时间比较。谢谢更新!不要使用
input
作为变量名。@AshwiniChaudhary说得好,谢谢!谢谢我自己也非常接近这一点(没有发布),但没有得到正确的逻辑部分。你知道这是否比@Ashwini的回答更有效?@blazetopher刚刚添加了一个时间比较。谢谢更新!不要使用
input
作为变量名。@AshwiniChaudhary说得好,谢谢!