Python - How to convert only numbers in a mixed list into float?

后端 未结 4 1598
不思量自难忘°
不思量自难忘° 2020-12-06 23:47

I\'m trying to learn Python and i have a problem, so if i have something like that:

data_l = [\'data\', \'18.8\', \'17.9\', \'0.0\']

How d

4条回答
  •  -上瘾入骨i
    2020-12-07 00:00

    You can use the str.isdigit method and a list comprehension:

    list = [int(s) if s.isdigit() else s for s in list]
    

    Here you have a live example

提交回复
热议问题