我有一個(gè)這樣的數(shù)據(jù)示例列表:list_ = [ (['0.640', '0.630', '0.64'], ['0.61', '0.65', '0.53']), (['20.00', '21.00', '21.00'], ['21.00', '22.00', '22.00']), (['0.025', '0.025', '0.026'], ['0.150', '0.150', '0.130'])] 我試圖將元組中的所有列表合并到元組中,這將是元組列表的結(jié)果。現(xiàn)在我想得到一個(gè)合并列表如下output = [ ('0.640', '0.630', '0.64', '0.61', '0.65', '0.53'), ('20.00', '21.00', '21.00', '21.00', '22.00', '22.00'), ('0.025', '0.025', '0.026', '0.150', '0.150', '0.130')]# or output = [ ['0.640', '0.630', '0.64', '0.61', '0.65', '0.53'], ['20.00', '21.00', '21.00', '21.00', '22.00', '22.00'], ['0.025', '0.025', '0.026', '0.150', '0.150', '0.130']]任何幫助表示贊賞。提前致謝!
3 回答

守著星空守著你
TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊
from itertools import chain output = [tuple(chain.from_iterable(t)) for t in list_]
使用chain
來自itertools
.

料青山看我應(yīng)如是
TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊
列表理解
[[item for internal_list_ in tuple_ for item in internal_list_] for tuple_ in list_]
麻木
np.array(list_).reshape((len(list_), -1))

ITMISS
TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊
output = [x[0]+x[1] for x in list_]
如果您想要一個(gè)通用的解決方案,則不必itertools
像其他人建議的那樣在這種情況下導(dǎo)入。這適用于 n 元組:
output = [sum([*x], []) for x in list_]
當(dāng)您沒有數(shù)千個(gè)列表時(shí),此解決方案將非常出色,但其他情況下則較差。
添加回答
舉報(bào)
0/150
提交
取消