Python 在每个列表元素上调用 int() 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3371269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call int() function on every list element?
提问by Silver Light
I have a list with numeric strings, like so:
我有一个包含数字字符串的列表,如下所示:
numbers = ['1', '5', '10', '8'];
I would like to convert every list element to integer, so it would look like this:
我想将每个列表元素转换为整数,所以它看起来像这样:
numbers = [1, 5, 10, 8];
I could do it using a loop, like so:
我可以使用循环来做到这一点,如下所示:
new_numbers = [];
for n in numbers:
new_numbers.append(int(n));
numbers = new_numbers;
Does it have to be so ugly? I'm sure there is a more pythonic way to do this in a one line of code. Please help me out.
有必要这么丑吗?我确信在一行代码中有一种更 Pythonic 的方法来做到这一点。请帮帮我。
采纳答案by adamk
回答by Mark Byers
回答by Nick Dandoulakis
Another way,
其它的办法,
for i, v in enumerate(numbers): numbers[i] = int(v)
回答by Tim McNamara
If you are intending on passing those integers to a function or method, consider this example:
如果您打算将这些整数传递给函数或方法,请考虑以下示例:
sum(int(x) for x in numbers)
This construction is intentionally remarkably similar to list comprehensions mentioned by adamk. Without the square brackets, it's called a generator expression, and is a very memory-efficient way of passing a list of arguments to a method. A good discussion is available here: Generator Expressions vs. List Comprehension
这种构造有意地与 adamk 提到的列表推导式非常相似。没有方括号,它被称为生成器表达式,并且是将参数列表传递给方法的一种非常节省内存的方式。这里有一个很好的讨论:生成器表达式与列表理解
回答by renatopp
回答by zhukovgreen
Another way to make it in Python 3:
在 Python 3 中实现它的另一种方法:
numbers = [*map(int, numbers)]
numbers = [*map(int, numbers)]
回答by Jim
Thought I'd consolidate the answers and show some timeitresults.
以为我会合并答案并显示一些timeit结果。
Python 2 sucks pretty bad at this, but mapis a bit faster than comprehension.
Python 2 在这方面很糟糕,但map比理解要快一些。
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import timeit
>>> setup = """import random
random.seed(10)
l = [str(random.randint(0, 99)) for i in range(100)]"""
>>> timeit.timeit('[int(v) for v in l]', setup)
116.25092001434314
>>> timeit.timeit('map(int, l)', setup)
106.66044823117454
Python 3 is over 4x faster by itself, but converting the mapgenerator object to a list is still faster than comprehension, and creating the list by unpacking the mapgenerator (thanks Artem!) is slightly faster still.
Python 3 本身的速度提高了 4 倍以上,但将map生成器对象转换为列表仍然比理解更快,并且通过解包map生成器(感谢 Artem!)来创建列表仍然稍微快一些。
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import timeit
>>> setup = """import random
random.seed(10)
l = [str(random.randint(0, 99)) for i in range(100)]"""
>>> timeit.timeit('[int(v) for v in l]', setup)
25.133059591551955
>>> timeit.timeit('list(map(int, l))', setup)
19.705547827217515
>>> timeit.timeit('[*map(int, l)]', setup)
19.45838406513076
Note: In Python 3, 4 elements seems to be the crossover point (3 in Python 2) where comprehension is slightly faster, though unpacking the generator is still faster than either for lists with more than 1 element.
注意:在 Python 3 中,4 个元素似乎是交叉点(Python 2 中的 3 个),其中理解速度稍快,但对于具有超过 1 个元素的列表,解包生成器仍然比任何一个都快。

