从元组到1d数组

时间:2017-04-14 13:25:53

标签: python

我有这个列表[(1,),(2,),(3,),(4,)] 如何将其转换为1d数组[1,2,3,4]

3 个答案:

答案 0 :(得分:3)

您可以解压缩所有元组:

>>> [e for e, in [(1,),(2,),(3,),(4,)]]
[1, 2, 3, 4]

答案 1 :(得分:1)

test = [(1,),(2,),(3,),(4,)]
single = []

for i in test:
    single.append(i[0])

print(single)

输出

[1, 2, 3, 4]

答案 2 :(得分:0)

i = [(1,),(2,),(3,),(4,)]
print([tup for tups in i for tup in tups])

输出 -

[1, 2, 3, 4]