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

后端 未结 5 547
悲&欢浪女
悲&欢浪女 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:17

    You could also use list comprehensions:

    new = [int(i) for i in old]
    

    Or the map() builtin function:

    new = map(int, old)
    

    Or the itertools.imap() function, which will provide a speedup in some cases but in this case just spits out an iterator, which you will need to convert to a list (so it'll probably take the same amount of time):

    import itertools as it
    new = list(it.imap(int, old))
    

提交回复
热议问题