[英][] and list() and 'int' object is not iterable in Python
Let's say I have an integer I want to make a list.假设我有一个 integer 我想列出一个清单。
This works这有效
[1]
However, this results in 'int' object is not iterable但是,这会导致 'int' object is not iterable
list(1)
What is the difference between []
and list()
and when is it appropriate to use each? []
和list()
有什么区别,什么时候适合使用它们?
list
is a built-in and []
are operators that can be used for, for example, either list comprehension or for list initialisation (which is what you're doing), but also for accessing certain indices in containers, etc. So you're kind of comparing apples and oranges. list
是内置的, []
是运算符,可用于例如列表理解或列表初始化(这是您正在做的),也可用于访问容器中的某些索引等。所以你'有点比较苹果和橘子。
Between list
with generator expressions and list comprehension, the latter is actually faster:在带有生成器表达式的
list
和列表推导之间,后者实际上更快:
$ python3 -m timeit '[_ for _ in range(1000)]'
10000 loops, best of 5: 29.4 usec per loop
$ python3 -m timeit 'list(_ for _ in range(1000))'
5000 loops, best of 5: 42.9 usec per loop
If we disassemble into bytecode we get:如果我们反汇编成字节码,我们会得到:
>>> dis.dis('[_ for _ in range(10)]')
1 0 LOAD_CONST 0 (<code object <listcomp> at 0x10d0647c0, file "<dis>", line 1>)
2 LOAD_CONST 1 ('<listcomp>')
4 MAKE_FUNCTION 0
6 LOAD_NAME 0 (range)
8 LOAD_CONST 2 (10)
10 CALL_FUNCTION 1
12 GET_ITER
14 CALL_FUNCTION 1
16 RETURN_VALUE
Disassembly of <code object <listcomp> at 0x10d0647c0, file "<dis>", line 1>:
1 0 BUILD_LIST 0
2 LOAD_FAST 0 (.0)
>> 4 FOR_ITER 8 (to 14)
6 STORE_FAST 1 (_)
8 LOAD_FAST 1 (_)
10 LIST_APPEND 2
12 JUMP_ABSOLUTE 4
>> 14 RETURN_VALUE
list
plus a generator expression list
加上一个生成器表达式>>> dis.dis('list(_ for _ in range(10))')
1 0 LOAD_NAME 0 (list)
2 LOAD_CONST 0 (<code object <genexpr> at 0x10d0647c0, file "<dis>", line 1>)
4 LOAD_CONST 1 ('<genexpr>')
6 MAKE_FUNCTION 0
8 LOAD_NAME 1 (range)
10 LOAD_CONST 2 (10)
12 CALL_FUNCTION 1
14 GET_ITER
16 CALL_FUNCTION 1
18 CALL_FUNCTION 1
20 RETURN_VALUE
Disassembly of <code object <genexpr> at 0x10d0647c0, file "<dis>", line 1>:
1 0 LOAD_FAST 0 (.0)
>> 2 FOR_ITER 10 (to 14)
4 STORE_FAST 1 (_)
6 LOAD_FAST 1 (_)
8 YIELD_VALUE
10 POP_TOP
12 JUMP_ABSOLUTE 2
>> 14 LOAD_CONST 0 (None)
16 RETURN_VALUE
So outside of having to look up the name ( list
, LOAD_NAME
), it seems to be mostly down to internal design;因此,除了必须查找名称(
list
, LOAD_NAME
)之外,它似乎主要取决于内部设计; that the generator expression is popping indicates a stack.生成器表达式正在弹出表示堆栈。
Compare this with what you were doing:将此与您正在执行的操作进行比较:
>>> dis.dis('[1]')
1 0 LOAD_CONST 0 (1)
2 BUILD_LIST 1
4 RETURN_VALUE
>>> dis.dis('list(1)')
1 0 LOAD_NAME 0 (list)
2 LOAD_CONST 0 (1)
4 CALL_FUNCTION 1
6 RETURN_VALUE
First one builds a list, while the second is (after looking up the name) trying to call the function list
.第一个建立一个列表,而第二个是(在查找名称之后)试图调用 function
list
。
list()
takes an Iterable
(for instance: instances of set
, dict
, tuple
) as argument, []
takes an explicit listing of the elements or a comprehension . list()
将Iterable
(例如: set
、 dict
、 tuple
的实例)作为参数, []
采用元素的显式列表或理解。
I don't think that you can use an integer with that我不认为你可以使用 integer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.