简体   繁体   English

在 Python 中创建一个空列表

[英]Creating an empty list in Python

What is the best way to create a new empty list in Python?在 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.代码可读性——哪一个是标准约定。

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 .我更喜欢[] ,但一些知识渊博的人,比如 Alex Martelli,更喜欢list()因为它是可发音的

list() is inherently slower than [] , because list()本质上比[]慢,因为

  1. there is symbol lookup (no way for python to know in advance if you did not just redefine list to be something else!),有符号查找(如果您不只是将列表重新定义为其他东西,python 无法提前知道!),

  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.然后它必须检查是否传递了可迭代参数(因此它可以创建包含元素的列表)ps。 none in our case but there is "if" check在我们的情况下没有,但有“如果”检查

In most cases the speed difference won't make any practical difference though.在大多数情况下,速度差异不会产生任何实际差异。

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?创建一个包含项目的列表应该与创建一个没有项目的列表大致相同,为什么会有区别?

I do not really know about it, but it seems to me, by experience, that jpcgt is actually right.我真的不知道它,但在我看来,根据经验, jpcgt 实际上是正确的。 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, eg在解释器中,然后调用 t 会给我没有任何列表的“t”,如果我附加其他内容,例如

t = t.append(2)

I get the error "'NoneType' object has no attribute 'append'".我收到错误“'NoneType' 对象没有属性 'append'”。 If, however, I create the list by但是,如果我通过以下方式创建列表

t = list() # explicit instantiation

then it works fine.然后它工作正常。

Just to highlight @Darkonaut answer because I think it should be more visible.只是为了突出@Darkonaut 的答案,因为我认为它应该更明显。

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) . new_list = []new_list = list()都很好(忽略性能),但append()返回None ,因此您不能执行new_list = new_list.append(something)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM