What is the easiest way to convert list with str into list with int?

后端 未结 5 532
悲&欢浪女
悲&欢浪女 2020-12-10 02:29

What is the easiest way to convert list with str into list with int in Python? For example, we have to convert [\'1\', \'2\', \'3\']

5条回答
  •  伪装坚强ぢ
    2020-12-10 03:16

    If your strings are not only numbers (ie. u''), you can use :

    new = [int(i) for i in ["1", "2", "3"] if isinstance(i, int) or isinstance(i, (str, unicode)) and i.isnumeric()]
    

提交回复
热议问题