How to convert strings numbers to integers in a list?

后端 未结 4 1638
[愿得一人]
[愿得一人] 2020-12-05 08:24

I have a list say:

[\'batting average\', \'306\', \'ERA\', \'1710\']

How can I convert the intended numbers without touching the strings?

4条回答
  •  [愿得一人]
    2020-12-05 09:09

    Try this:

    def convert( someList ):
        for item in someList:
            try:
                yield int(item)
            except ValueError:
                yield item
    
    newList= list( convert( oldList ) )
    

提交回复
热议问题