我有这个:
[[['0'], ['0'], ['0'], ['0'], ['0'], ['0.0178885743420442']], [['0'], ['0'], ['0'], ['0'], ['0.00189664199330356'], ['0.0105739740488949']], [['0'], ['0'], ['0'], ['0'], ['0'], ['0.0131745569045574']]]
最后,我想得到:
[['0', '0', '0', '0', '0', '0.0178885743420442'], ['0', '0', '0', '0', '0.00189664199330356', '0.0105739740488949'], ['0', '0', '0', '0', '0', '0.0131745569045574']]
我尝试使用加入但没有任何事情发生 感谢
答案 0 :(得分:4)
>>> from itertools import chain
>>> list(chain(*mylist))
['0', '0', '0', '0', '0', '0.0216939116219239', '0.44748960163182', '1.47290267132899', '2.34321826311047', '2.99520324761532', '3.11538509056595', '3.46600327678834', '2.80910123913849']
答案 1 :(得分:2)
只需做一个列表理解:
x = [['0'], ['0'], ['0'], ['0'], ['0'], ['0.0216939116219239'], ['0.44748960163182'], ['1.47290267132899'], ['2.34321826311047'], ['2.99520324761532'], ['3.11538509056595'], ['3.46600327678834'], ['2.80910123913849']]
print [j for i in x for j in i]
输出:
['0', '0', '0', '0', '0', '0.0216939116219239', '0.44748960163182', '1.47290267132899', '2.34321826311047', '2.99520324761532', '3.11538509056595', '3.46600327678834', '2.80910123913849']
或者
print [ast.literal_eval(j) for i in x for j in i] # for list of floats and integers
输出:
[0, 0, 0, 0, 0, 0.0216939116219239, 0.44748960163182, 1.47290267132899, 2.34321826311047, 2.99520324761532, 3.11538509056595, 3.46600327678834, 2.80910123913849]
答案 2 :(得分:0)
这样可行(获取每个子列表的第一个元素):
[x[0] for x in list]
答案 3 :(得分:0)
您可以将Python列表理解与chain.from_iterable
中的itertools
函数结合使用,如下所示:
from itertools import chain
data = [[['0'], ['0'], ['0'], ['0'], ['0'], ['0.0178885743420442']], [['0'], ['0'], ['0'], ['0'], ['0.00189664199330356'], ['0.0105739740488949']], [['0'], ['0'], ['0'], ['0'], ['0'], ['0.0131745569045574']]]
output = [list(chain.from_iterable(x)) for x in data]
print output
将每个子子条目组合成子条目。
按如下方式为您提供所需的输出:
[['0', '0', '0', '0', '0', '0.0178885743420442'], ['0', '0', '0', '0', '0.00189664199330356', '0.0105739740488949'], ['0', '0', '0', '0', '0', '0.0131745569045574']]