Convertir todas las cadenas de una lista a int [duplicate]


Posible Duplicado:
Cómo convertir cadenas en enteros en python?
Cómo convertir una cadena en un entero en python

En python, quiero convertir todas las cadenas de una lista a ints.

Así que si tengo:

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

Cómo lo hago:

results = [1, 2, 3]
Author: Community, 2011-09-10

2 answers

Utilice la función map (en py2):

results = map(int, results)

En py3:

results = list(map(int, results))
 825
Author: cheeken,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-23 05:00:07

Use una lista comprensión:

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

Por ejemplo

>>> results = ["1", "2", "3"]
>>> results = [int(i) for i in results]
>>> results
[1, 2, 3]
 258
Author: Chris Vig,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-09-10 01:08:43