我有一個這樣的數據示例列表: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'])] 我試圖將元組中的所有列表合并到元組中,這將是元組列表的結果?,F在我想得到一個合并列表如下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貢獻1799條經驗 獲得超8個贊
from itertools import chain output = [tuple(chain.from_iterable(t)) for t in list_]
使用chain
來自itertools
.

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

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