提问人:Prachi 提问时间:10/14/2022 更新时间:10/15/2022 访问量:23
跳过文件中的字符串并在列表中附加浮点数
To skip the strings from a file and append floating numbers in list
答:
0赞
Rahul K P
10/15/2022
#1
如果您只获取列表中的数字
lst = ['NaN','37','45','46','a','32']
lst = [i for i in lst if i.isdigit()]
结果:
['37', '45', '46', '32']
如果要将上述列表转换为浮点数
lst = list(map(float, last))
结果:
[37.0, 45.0, 46.0, 32.0]
如果你想删除项目,你可以这样做,try-except
In [1]: for item in lst[:]:
...: try:
...: float(item)
...: except TypeError:
...: lst.remove(item)
...:
In [2]: lst
Out[2]: ['37', '45', '46', '32']
评论
a