如何将元组列表中的所有元素循环到空列表中?
例如:
tup_Before = [(69592, 69582), (69582, 69518), (69518, 69532), (69532, 69525)]
tup_After = [69592, 69582, 69582, 69518, 69518, 69532, 69532, 69525]
答案 0 :(得分:1)
列表理解:
tup_after = [v for t in tup_Before for v in t]
或使用itertools.chain.from_iterable()
:
from itertools import chain
tup_after = list(chain.from_iterable(tup_Before))
演示:
>>> tup_Before = [(69592, 69582), (69582, 69518), (69518, 69532), (69532, 69525)]
>>> [v for t in tup_Before for v in t]
[69592, 69582, 69582, 69518, 69518, 69532, 69532, 69525]
>>> from itertools import chain
>>> list(chain.from_iterable(tup_Before))
[69592, 69582, 69582, 69518, 69518, 69532, 69532, 69525]
答案 1 :(得分:0)
另外,最不明确的答案是:
[l for l in l for l in l]
其中l
是您的列表名称
答案 2 :(得分:0)
您可以尝试使用itertools.chain()
并使用*
解压缩:
import itertools
new_tup = list(itertools.chain(*tup_before))
<强>演示:强>
>>> print new_tup
[69592, 69582, 69582, 69518, 69518, 69532, 69532, 69525]