[[' 19'],[' 19',' 21']]在python中将其转换为[19,19,21]

时间:2018-01-28 16:50:54

标签: python pandas numpy

如何删除list内部列表(Double Square Brackets)并将其作为整数类型列表。

我尝试一次选择一个元素:

enter code here
x = [['19'], ['19', '21']]
x = (int(x) for x in x)

但相反,它会产生<generator object>

2 个答案:

答案 0 :(得分:0)

如果您将列表添加到一起,您将获得所需的结果,如:

代码:

sum(x, [])

测试代码:

x = [['19'], ['19', '21']]

print(sum(x, []))

结果:

['19', '19', '21']

答案 1 :(得分:0)

您只需要:

final_list = [i for sub_list in x for i in sub_list]