示例:
Column 1
[1, 3, " "]
[2, " ", 3]
etc.
有一个快速的列表理解功能,我可以只保留整数吗?
答案 0 :(得分:0)
您可能正在寻找类似的东西:
a = [1, 3, " "]
b = [i for i in a if i != " "]
print(b) #> [1, 3]
如果要添加其他空格以删除:
a = [1, 3, " ", ""]
b = [i for i in a if i not in (" ", "")]
如果您只想添加int
(如果有str
,float
等,此方法将不通用):
a = [1, 3, " ", ""]
b = [i for i in a if isinstance(i, int)]