关于python:将元组列表转换为简单的平面列表

Converting a list of tuples into a simple flat list

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
How do I convert a tuple of tuples to a one-dimensional list using list comprehension?

假设我有以下元组列表:

1
[(1,2), (1,3), (1,4), (1,5), (1,6)]

我正在尝试将其转换为如下所示的简单列表:

1
[1,2,1,3,1,4,1,5,1,6]

如何将它转换为一个简单的列表(如上面的列表),而不必遍历每个元素并将项目逐个添加到另一个列表中?

有没有什么快速而有效的方法可以做到这一点而不需要实际地遍历元组的原始列表,或者有没有一些内置的函数/方法可以做到这一点?


1
2
3
4
5
lst = [(1,2), (1,3), (1,4), (1,5), (1,6)]

import itertools
list(itertools.chain(*lst))
# [1, 2, 1, 3, 1, 4, 1, 5, 1, 6]

可选地:

1
2
[e for l in lst for e in l]
# [1, 2, 1, 3, 1, 4, 1, 5, 1, 6]


"Fundamentally, which one is faster? Using the"itertools" module, or using a list comprehension? I'm basically trying to improve my computation speed here." - @davidadamojr

我做了一些测试,发现下面的代码实际上更快。

1
2
list_ = [(1, 2), (1, 3), (1, 4), (1, 5), (1, 6)]
list(sum(list_, ()))

如果我错了,有人纠正我。

下面是一些测试。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
>>> list_ = [(1, 2), (1, 3), (1, 4), (1, 5), (1, 6)]
>>>
>>> operation_1 = lambda: [tuple_item for tuple_ in list_ for tuple_item in tuple_]
>>> def operation_2 ():
        final_list = []
        for tuple_ in list_:
            for tuple_item in tuple_:
                final_list.append(tuple_item)
        return final_list

>>> operation_3 = lambda: reduce(list.__add__, map(list, list_))
>>> def operation_4 ():
        import itertools
        return list(itertools.chain(*list_))

>>> operation_5 = lambda: list(sum(list_, ()))
>>>
>>> operation_1()
[1, 2, 1, 3, 1, 4, 1, 5, 1, 6]
>>> operation_2()
[1, 2, 1, 3, 1, 4, 1, 5, 1, 6]
>>> operation_3()
[1, 2, 1, 3, 1, 4, 1, 5, 1, 6]
>>> operation_4()
[1, 2, 1, 3, 1, 4, 1, 5, 1, 6]
>>> operation_5()
[1, 2, 1, 3, 1, 4, 1, 5, 1, 6]
>>>
>>> import timeit
>>>
>>> print('operation_1 completed in %s seconds.' % (timeit.timeit(operation_1)))
operation_1 completed in 1.57890490223 seconds.
>>> print('operation_2 completed in %s seconds.' % (timeit.timeit(operation_2)))
operation_2 completed in 2.90350501659 seconds.
>>> print('operation_3 completed in %s seconds.' % (timeit.timeit(operation_3)))
operation_3 completed in 5.08437990236 seconds.
>>> print('operation_4 completed in %s seconds.' % (timeit.timeit(operation_4)))
operation_4 completed in 3.85125378138 seconds.
>>> print('operation_5 completed in %s seconds.' % (timeit.timeit(operation_5)))
operation_5 completed in 1.2623826489 seconds.


使用chain.from_iterable,因为它可以通过缓慢地遍历列表来避免不必要的一次性解包(这会导致冗余内存消耗):

1
2
3
4
>>> import itertools
>>> L = [(1,2), (1,3), (1,4), (1,5), (1,6)]
>>> list(itertools.chain.from_iterable(L))
[1, 2, 1, 3, 1, 4, 1, 5, 1, 6]

以下是实现这一点的最佳方法,从性能和独立性方面考虑,特别是像itertools这样的模块:

1
2
3
>>> l = [(1,2), (1,3), (1,4), (1,5), (1,6)]
>>> reduce(list.__add__,map(list,l))
[1, 2, 1, 3, 1, 4, 1, 5, 1, 6]