Hướng dẫn python flatten tuple of tuple - trăn làm phẳng tuple của tuple

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)
 


Làm thế nào để bạn làm phẳng một tuple trong Python?

Một phương thức để làm phẳng các bộ dữ liệu của một danh sách là bằng cách sử dụng phương thức SUM () với ham muốn trống sẽ trả về tất cả các phần tử của tuple làm giá trị riêng lẻ trong danh sách.using the sum() method with empty lust which will return all elements of the tuple as individual values in the list.

Làm cách nào để lập danh sách trong danh sách các bộ dữ liệu?

Nếu bạn đang vội, đây là câu trả lời ngắn gọn: Sử dụng câu lệnh Danh sách hiểu [list (x) cho x trong các bộ dữ liệu] để chuyển đổi từng tuple trong Tuples thành một danh sách.Điều này cũng hoạt động cho một danh sách các bộ dữ liệu với số lượng các yếu tố khác nhau.Use the list comprehension statement [list(x) for x in tuples] to convert each tuple in tuples to a list. This also works for a list of tuples with a varying number of elements.

Làm thế nào để bạn nối một tuple?

Chụp thêm một tuple python với bộ phận giải nén toán tử giải nén, *, được sử dụng để truy cập tất cả các mục trong một đối tượng container, chẳng hạn như một tuple.Để nối một giá trị, trước tiên chúng tôi giải nén tất cả các giá trị của bộ đầu tiên, và sau đó bao gồm giá trị hoặc giá trị mới.In order to append a value, we first unpack all the values of the first tuple, and then include the new value or values.