问题:在Python中创建一个空列表

在Python中创建新的空列表的最佳方法是什么?

l = [] 

要么

l = list()

我之所以这样问是因为两个原因:

  1. 技术原因,关于哪个更快。(创建一个类会导致开销吗?)
  2. 代码可读性-这是标准约定。

What is the best way to create a new empty list in Python?

l = [] 

or

l = list()

I am asking this because of two reasons:

  1. Technical reasons, as to which is faster. (creating a class causes overhead?)
  2. Code readability – which one is the standard convention.

回答 0

您可以通过以下方法测试哪段代码更快:

% python -mtimeit  "l=[]"
10000000 loops, best of 3: 0.0711 usec per loop

% python -mtimeit  "l=list()"
1000000 loops, best of 3: 0.297 usec per loop

但是,实际上,这种初始化很可能只是程序的一小部分,因此担心此初始化可能会出错。

可读性是非常主观的。我更喜欢[],但是像Alex Martelli这样的一些非常博学的人更喜欢,list()因为它很明显

Here is how you can test which piece of code is faster:

% python -mtimeit  "l=[]"
10000000 loops, best of 3: 0.0711 usec per loop

% python -mtimeit  "l=list()"
1000000 loops, best of 3: 0.297 usec per loop

However, in practice, this initialization is most likely an extremely small part of your program, so worrying about this is probably wrong-headed.

Readability is very subjective. I prefer [], but some very knowledgable people, like Alex Martelli, prefer list() because it is pronounceable.


回答 1

list()本质上比慢[],因为

  1. 有符号查找(python不能事先知道您是否不只是将列表重新定义为其他内容!),

  2. 有函数调用,

  3. 然后它必须检查是否传递了可迭代的参数(以便它可以使用其中的元素创建列表)。在我们的情况下没有,但是有“如果”检查

在大多数情况下,速度差异不会产生任何实际差异。

list() is inherently slower than [], because

  1. there is symbol lookup (no way for python to know in advance if you did not just redefine list to be something else!),

  2. there is function invocation,

  3. then it has to check if there was iterable argument passed (so it can create list with elements from it) ps. none in our case but there is “if” check

In most cases the speed difference won’t make any practical difference though.


回答 2

我用[]

  1. 速度更快,因为列表符号是短路。
  2. 创建包含项目的列表应该创建不包含项目的列表大致相同,为什么会有区别呢?

I use [].

  1. It’s faster because the list notation is a short circuit.
  2. Creating a list with items should look about the same as creating a list without, why should there be a difference?

回答 3

我并不是很了解,但是根据我的经验,jpcgt实际上是正确的。以下示例:如果我使用以下代码

t = [] # implicit instantiation
t = t.append(1)

在解释器中,然后调用t给我“ t”,不带任何列表,如果我附加其他内容,例如

t = t.append(2)

我收到错误“’NoneType’对象没有属性’append’”。但是,如果我通过以下方式创建列表

t = list() # explicit instantiation

然后就可以了

I do not really know about it, but it seems to me, by experience, that jpcgt is actually right. Following example: If I use following code

t = [] # implicit instantiation
t = t.append(1)

in the interpreter, then calling t gives me just “t” without any list, and if I append something else, e.g.

t = t.append(2)

I get the error “‘NoneType’ object has no attribute ‘append'”. If, however, I create the list by

t = list() # explicit instantiation

then it works fine.


回答 4

只是强调@Darkonaut 答案因为我认为它应该更明显。

new_list = []new_list = list()两者都很好(忽略性能),但append()返回None,结果您无法做new_list = new_list.append(something

这种返回类型的决定让我感到非常困惑。uck

Just to highlight @Darkonaut answer because I think it should be more visible.

new_list = [] or new_list = list() are both fine (ignoring performance), but append() returns None, as result you can’t do new_list = new_list.append(something).


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。