python中的[None]和[]有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36928602/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:34:11  来源:igfitidea点击:

what is difference between [None] and [] in python?

pythonlist

提问by minssi

I think [None] is same as [], but in my test , maybe there is something...

我认为 [None] 与 [] 相同,但在我的测试中,也许有些东西......

>>>print len([])
0
>>>print len([None])
1

when should i use the None ? and []

我什么时候应该使用 None ?和 []

and another interesting question

还有另一个有趣的问题

>>>c= []
>>>d= []
>>>print c is d
False
>>>a= 1
>>>b=1
print a is b
True

why empty list's id granting that is different?

为什么空列表的 id 授予不同?

回答by gnicholas

[]is an empty list

[]是一个空列表

[None]is a list with one element. That one element is None

[None]是一个包含一个元素的列表。那一个元素是None

ischecks for reference equality. If both objects refer to the same object by referencethen iswill return true.

is检查引用相等性。如果两个对象通过引用引用同一个对象,则is返回 true。

a = []
b = a
a is [] #false
a is b  #true

回答by Frank Bryce

[None]does not mean that there is nothing in the list. Noneis a keyword in pythonwhich has a special meaning. It is like NILor NULLin other languages.

[None]并不意味着列表中没有任何内容。 None是python中的一个关键字,具有特殊含义。它就像NILNULL在其他语言中一样。

When you say [None], you are saying "I would like to have a list which contains the special object called None". This is different than saying "I would like a list which contains no elements" (by typing []).

当您说时[None],您是在说“我想要一个包含名为None”的特殊对象的列表。这与说“我想要一个不包含任何元素的列表”(通过键入[])不同。

回答by John Mulder

Question 1:

问题 1:

None is an object. It is of type "NoneType".
This can be seen by doing something like this in the terminal:

None 是一个对象。它是“NoneType”类型。
这可以通过在终端中执行以下操作来看到:

>>> type(None)
<type 'NoneType'>

So, when you put this object in a list, the list has one element.

所以,当你把这个对象放在一个列表中时,这个列表只有一个元素。

Question 2:

问题2:

The assignment operator in Python, =, is used to attach a name to an object. In the case of immutable objects, like integers, multiple names can be attached to the same object. That is what you are doing with aand b. So, when you test their identity, using the isoperator, you see that the two names point to the identical object.

Python 中的赋值运算符=,用于将名称附加到对象。对于不可变对象(如整数),可以将多个名称附加到同一个对象。这就是你正在用aand做的事情b。因此,当您使用is运算符测试它们的身份时,您会看到这两个名称指向相同的对象。

Alternatively, when you attach a name to a newly created list (which you created with the []operator) it is a different list each time.

或者,当您将名称附加到新创建的列表(由[]操作员创建)时,它每次都是不同的列表。

回答by MSeifert

Noneis a valid element, but you can treat it like a stub or placeholder. So it counts as an element inside the list even if there is only a None.

None是一个有效元素,但您可以将其视为存根或占位符。因此,即使只有一个None.

For (equality) comparisons you shouldn't use is. Use ==!

对于(相等)比较,您不应该使用is. 使用==

Because iscan lead to strange behaviour if you don't know exactly when and how to use it. For example:

因为is如果您不确切知道何时以及如何使用它,可能会导致奇怪的行为。例如:

>>> 1900 is 1900
True

>>> a = 1900
>>> b = 1900
>>> a is b
False

>>> a, b = 1900, 1900
>>> a is b
True

This rather strange behaviour is explained for example in this question: Why does Python handle '1 is 1**2' differently from '1000 is 10**3'?

例如,在这个问题中解释了这种相当奇怪的行为:为什么 Python 处理 '1 is 1**2' 与 '1000 is 10**3' 不同?

This won't happen when you use ==:

使用时不会发生这种情况==

>>> a == b
True
>>> 1900 == 1900
True

like one would expect.

正如人们所期望的那样。

回答by trans1st0r

You want to use Noneto imply that there is no valid object. You want to use []to imply an object that is of type list and has no elements.

您想用None来暗示没有有效的对象。您想使用[]来暗示一个列表类型且没有元素的对象。

[None]is a list with one element which is None

[None]是一个包含一个元素的列表 None

>>>c= []  # This is a new list object
>>>d= []  # This is another new list object

In Python , x is yis used to check whether xand yare the same objects. Here, cand dpoint to different list objects. so,

在 Python 中,x is y用于检查x和是否y是相同的对象。在这里,cd指向不同的列表对象。所以,

>>>print c is d
False

is expected.

是期待。

On the other hand,

另一方面,

>>>c= []  # This is a new list object
>>>d = c  # This is the same object as c
>>>print c is d
True

Here, a and b are primitives, not objects

这里,a 和 b 是基元,而不是对象

>>>a= 1
>>>b=1

So, this is expected:

所以,这是预期的:

print a is b
True