可能重复:
How to convert strings into integers in python?
How to convert a string list into an integer in python转换所有字符串列表来诠释
在蟒蛇,我想所有的字符串转换列表中以整数。
所以,如果我有:
results = ['1', '2', '3']
如何使它:
results = [1, 2, 3]
可能重复:
How to convert strings into integers in python?
How to convert a string list into an integer in python转换所有字符串列表来诠释
在蟒蛇,我想所有的字符串转换列表中以整数。
所以,如果我有:
results = ['1', '2', '3']
如何使它:
results = [1, 2, 3]
使用地图功能(在PY2):
results = map(int, results)
在PY3:
results = list(map(int, results))
我想指出,pylint不鼓励使用'地图',所以准备无论如何,如果你曾经使用过这个标准,就可以使用列表解析。 :) – ThorSummoner
反过来(将int列表转换为字符串列表):map(str,results) –
您可以简化这个答案:只要总是使用'list(map(int,results))',它适用于任何Python版本。 – mvp
使用列表理解:
results = [int(i) for i in results]
例如,
>>> results = ["1", "2", "3"]
>>> results = [int(i) for i in results]
>>> results
[1, 2, 3]
列表理解也很棒。到OP - 看到这里看到一个很好的地图和列表理解的对比:http://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map – cheeken
看到这个:http://stackoverflow.com/questions/642154/how-to-convert-strings-into-integers-in-python –
遗憾的是应该是一个没有答案的注释。看到这个答案︰http://stackoverflow.com/questions/642154/how-to-convert-strings-into-integers-in-python –