Convert all strings in a list to int

前端 未结 4 731
夕颜
夕颜 2020-11-22 01:49

In Python, I want to convert all strings in a list to integers.

So if I have:

results = [\'1\', \'2\', \'3\']

How do I make it:

4条回答
  •  星月不相逢
    2020-11-22 02:08

    Use a list comprehension:

    results = [int(i) for i in results]
    

    e.g.

    >>> results = ["1", "2", "3"]
    >>> results = [int(i) for i in results]
    >>> results
    [1, 2, 3]
    

提交回复
热议问题