How to convert list of intable strings to int

前端 未结 6 1371
我在风中等你
我在风中等你 2020-12-06 13:28

In Python, I want to convert a list of strings:

l = [\'sam\',\'1\',\'dad\',\'21\']

and convert the integers to integer types like this:

6条回答
  •  不知归路
    2020-12-06 13:42

    Use isdigit() to check each character in the string to see if it is a digit.

    Example:

    mylist = ['foo', '3', 'bar', '9']
    t = [ int(item) if item.isdigit() else item for item in mylist ] 
    print(t)
    

提交回复
热议问题