[英]Python extract elements from list of tuple
我正在尝试从元组列表中提取元素。 我得到了答案,但想知道有没有更好的方法? 代码:
pred_result_tuple = [('n02123045', 'tabby', 0.5681726)]
pred_result_list = [x[n] for x in pred_result_tuple for n in [0,1,2]]
print(pred_result_list)
['n02123045', 'tabby', 0.5681726]
尝试但失败:
print([x[n] for x,n in zip(pred_result_tuple,[0,1,2])])
['n02123045']
import itertools
result = list(itertools.chain(*pred_result_tuple))
>>> print(result)
>>> ['n02123045', 'tabby', 0.5681726]
但是,如果您在list
中只有一个tuple
,您可以这样做:
result = list(*pred_result_tuple)
>>> print(result)
>>> ['n02123045', 'tabby', 0.5681726]
利用
Listoftuples=[(#elements)]
l=[]
for e in Listoftuples[0]:
l=l+[e]
print(l)
结果
[#elements]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.