Chỉ cần sử dụng sum
nếu bạn không có nhiều bộ dữ liệu.
>>> tupleOfTuples = [[1, 2], [3, 4], [5,]]
>>> sum[tupleOfTuples, []]
[1, 2, 3, 4, 5]
>>> list[sum[tupleOfTuples, []]] # if you really need a list
[1, 2, 3, 4, 5]
Nếu bạn có nhiều bộ dữ liệu, hãy sử dụng danh sách hiểu hoặc chain.from_iterable
để ngăn chặn hành vi bậc hai của sum
.
Micro-benchmarks:
Python 2.6
Dài bộ dữ liệu ngắn
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]' 10000 loops, best of 3: 134 usec per loop $ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]' 1000 loops, best of 3: 1.1 msec per loop $ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]' 10000 loops, best of 3: 60.1 usec per loop $ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]' 10000 loops, best of 3: 64.8 usec per loop
Bộ dữ liệu ngắn
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]' 10000 loops, best of 3: 65.6 usec per loop $ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]' 100000 loops, best of 3: 16.9 usec per loop $ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]' 10000 loops, best of 3: 25.8 usec per loop $ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]' 10000 loops, best of 3: 26.5 usec per loop
Python 3.1
Dài bộ dữ liệu ngắn
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]' 10000 loops, best of 3: 121 usec per loop $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]' 1000 loops, best of 3: 1.09 msec per loop $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]' 10000 loops, best of 3: 59.5 usec per loop $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]' 10000 loops, best of 3: 63.2 usec per loop
Bộ dữ liệu ngắn
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]' 10000 loops, best of 3: 66.1 usec per loop $ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]' 100000 loops, best of 3: 16.3 usec per loop $ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]' 10000 loops, best of 3: 25.4 usec per loop $ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]' 10000 loops, best of 3: 25.6 usec per loop
Observation:
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]' 10000 loops, best of 3: 65.6 usec per loop $ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]' 100000 loops, best of 3: 16.9 usec per loop $ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]' 10000 loops, best of 3: 25.8 usec per loop $ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]' 10000 loops, best of 3: 26.5 usec per loop
- Python 3.1
Đôi khi, trong khi làm việc với các bộ dữ liệu Python, chúng ta có thể gặp vấn đề trong đó chúng ta cần thực hiện việc làm phẳng các bộ dữ liệu, có danh sách là các yếu tố cấu thành của nó. Loại vấn đề này là phổ biến trong các lĩnh vực dữ liệu như học máy. Hãy để thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện.
Đầu vào: test_tuple = [[5], [6], [3], [8]] Đầu ra: [5, 6, 3, 8] Đầu vào: test_tuple = [[5, 7, 8]] Đầu ra: [5, 7, 8] : test_tuple = [[5], [6], [3], [8]] Output : [5, 6, 3, 8] Input : test_tuple = [[5, 7, 8]] Output : [5, 7, 8]
Phương pháp số 1: Sử dụng sum [] + tuple [] Sự kết hợp của các hàm trên có thể được sử dụng để giải quyết vấn đề này. Trong đó, chúng tôi thực hiện nhiệm vụ làm phẳng bằng Sum [], chuyển danh sách trống làm đối số của nó. & NBSP; The combination of above functions can be used to solve this problem. In this, we perform the task of flattening using sum[], passing empty list as its argument.
Python3
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
2$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
3 $ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
4$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
5$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
6$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
7$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
8$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
7$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
6$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 65.6 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.9 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.8 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 26.5 usec per loop
1__16$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 65.6 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.9 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.8 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 26.5 usec per loop
9$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
0$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
1 $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
2$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
3 $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
4$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
5$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
6$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
3 $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
1$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
9sum
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 66.1 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.3 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.4 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 25.6 usec per loop
1$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 65.6 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.9 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.8 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 26.5 usec per loop
9$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 66.1 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.3 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.4 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 25.6 usec per loop
3$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
1 $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
2$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
3 $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
4$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 66.1 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.3 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.4 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 25.6 usec per loop
8Đầu ra: & nbsp;
The original tuple : [[5, 6], [6, 7, 8, 9], [3]] The flattened tuple : [5, 6, 6, 7, 8, 9, 3]
& nbsp; Phương thức số 2: Sử dụng tuple [] + chuỗi.from_iterable [] Sự kết hợp của các hàm trên có thể được sử dụng để giải quyết vấn đề này. Trong đó, chúng tôi thực hiện nhiệm vụ làm phẳng bằng cách sử dụng from_iterable [] và chuyển đổi sang tuple bằng cách sử dụng tuple []. & Nbsp;Method #2 : Using tuple[] + chain.from_iterable[] The combination of above functions can be used to solve this problem. In this, we perform task of flattening using from_iterable[] and conversion to tuple using tuple[].
Python3
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 66.1 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.3 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.4 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 25.6 usec per loop
9 The original tuple : [[5, 6], [6, 7, 8, 9], [3]] The flattened tuple : [5, 6, 6, 7, 8, 9, 3]0
The original tuple : [[5, 6], [6, 7, 8, 9], [3]] The flattened tuple : [5, 6, 6, 7, 8, 9, 3]1
The original tuple : [[5, 6], [6, 7, 8, 9], [3]] The flattened tuple : [5, 6, 6, 7, 8, 9, 3]2
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
2$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
3 $ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
4$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
5$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
6$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
7$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
8$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
7$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
6$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 65.6 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.9 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.8 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 26.5 usec per loop
1__16$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 65.6 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.9 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.8 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 26.5 usec per loop
9$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
0$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
1 $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
2$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
3 $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
4$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
5$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
6$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
3 $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
1$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
9sum
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 66.1 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.3 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.4 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 25.6 usec per loop
1$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 65.6 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.9 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.8 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 26.5 usec per loop
9$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 66.1 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.3 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.4 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 25.6 usec per loop
3$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
1 $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
2$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
3 $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
4$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 66.1 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.3 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.4 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 25.6 usec per loop
8Đầu ra: & nbsp;
The original tuple : [[5, 6], [6, 7, 8, 9], [3]] The flattened tuple : [5, 6, 6, 7, 8, 9, 3]
& nbsp; Phương thức số 2: Sử dụng tuple [] + chuỗi.from_iterable [] Sự kết hợp của các hàm trên có thể được sử dụng để giải quyết vấn đề này. Trong đó, chúng tôi thực hiện nhiệm vụ làm phẳng bằng cách sử dụng from_iterable [] và chuyển đổi sang tuple bằng cách sử dụng tuple []. & Nbsp;
Python3
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
2$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
3 $ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
4$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
5$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
6$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
7$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
8$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
7$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
6$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 65.6 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.9 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.8 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 26.5 usec per loop
1__16$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 65.6 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.9 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.8 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 26.5 usec per loop
9$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
0$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
1 $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
2$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
3 $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
4$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
5$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
11$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
3$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
13$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
6$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
3 $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
1$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
9sum
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 66.1 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.3 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.4 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 25.6 usec per loop
1$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
18$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
19$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
11$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
3$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
1$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 134 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.1 msec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 60.1 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 64.8 usec per loop
23$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 65.6 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.9 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.8 usec per loop
$ python2.6 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 26.5 usec per loop
9$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 66.1 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.3 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.4 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 25.6 usec per loop
3$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
1 $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
2$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
3 $ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 121 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500' 'list[sum[tot, []]]'
1000 loops, best of 3: 1.09 msec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 59.5 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, 2], ]*500; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 63.2 usec per loop
4$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' '[element for tupl in tot for element in tupl]'
10000 loops, best of 3: 66.1 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]' 'list[sum[tot, []]]'
100000 loops, best of 3: 16.3 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain; ci = chain.from_iterable' 'list[ci[tot]]'
10000 loops, best of 3: 25.4 usec per loop
$ python3.1 -m timeit -s 'tot = [[1, ]*500, [2, ]*500]; from itertools import chain' 'list[chain[*tot]]'
10000 loops, best of 3: 25.6 usec per loop
8Đầu ra: & nbsp;
& nbsp; Phương thức số 2: Sử dụng tuple [] + chuỗi.from_iterable [] Sự kết hợp của các hàm trên có thể được sử dụng để giải quyết vấn đề này. Trong đó, chúng tôi thực hiện nhiệm vụ làm phẳng bằng cách sử dụng from_iterable [] và chuyển đổi sang tuple bằng cách sử dụng tuple []. & Nbsp;
The flattened tuple : [5, 6, 6, 7, 8, 9, 3]