简体   繁体   English

为什么Python列表的行为会根据声明的不同而有所不同?

[英]Why does Python list of lists behave differently depending on their declaration?

I am trying to create a list inside another list in Python. 我试图在Python中的另一个列表中创建一个列表。 I noticed that depending on the declaration the final (outer) list behaves differently. 我注意到,根据声明,最终(外部)列表的行为有所不同。

I tried to create list of lists in two different ways. 我尝试以两种不同的方式创建列表列表。 Both cases gave me varies results. 两种情况都给了我不同的结果。

#Case 1
def test():
    lt1 = lt2 = list()
    for i in range(0, 10):
        for j in range(0, 2):
            lt1.append(j);
        lt2.append(lt1);
        lt1 = [];
    print (lt2)

if __name__ == "__main__":
    test()
#Case 2
def test():
    lt1 = list()
    lt2 = list()
    for i in range(0, 10):
        for j in range(0, 2):
            lt1.append(j);
        lt2.append(lt1);
        lt1 = [];
    print (lt2)

if __name__ == "__main__":
    test()

In case 1 the output is [0, 1, [...], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]] 在情况1中,输出为[0,1,[...],[0,1],[0,1],[0,1],[0,1],[0,1],[0, 1],[0,1],[0,1],[0,1]]

In case 2 the output is [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]] which is the expected answer for my implementation. 在情况2中,输出为[[0,1],[0,1],[0,1],[0,1],[0,1],[0,1],[0,1],[ 0,1],[0,1],[0,1]]这是我实现的预期答案。

I wanted to know why the first code snippet acts differently. 我想知道为什么第一个代码段的行为不同。

It's because of the first line: 这是因为第一行:

>>> a = b = []
>>> a
[]
>>> b
[]
>>> a is b
True
>>> a = []
>>> b = []
>>> a is b
False
>>> 

With one line as in case 1, it contains the same object, so: 如果情况1中有一行,它包含相同的对象,因此:

>>> a = b = []
>>> a.append(1)
>>> a
[1]
>>> b
[1]
>>> 

Which doesn't happen with two lines: 两行不会发生这种情况:

>>> a = []
>>> b = []
>>> a.append(1)
>>> a
[1]
>>> b
[]
>>> 

So simply because the first line of case 1 has a and b that are the exact same objects, unlike the second case's fist line, that are same values, but different id ( id(a) == id(b) is the same as a is b ). 因此,仅仅因为案例1的第一行的ab是完全相同的对象,与第二种情况的第一行不同,它们是相同的值,但不同的idid(a) == id(b)a is b )。

暂无
暂无

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

相关问题 为什么 Python 3 for loop output 和行为不同? - Why does Python 3 for loop output and behave differently? 为什么.loc的行为会有所不同,具体取决于是打印还是分配值? - Why does .loc behave differently depending on whether values are printed or assigned? 为什么在Python中将迭代器设置为变量与直接在列表推导中使用迭代器的行为有所不同? - Why does setting an iterator to a variable in Python behave differently than using it directly in a list comprehension? 为什么Python范围对于列表变量和字符串变量的行为似乎不同? - Why the Python scope seems to behave differently for list variable and string variable? 为什么这个argparse代码在Python 2和3之间表现不同? - Why does this argparse code behave differently between Python 2 and 3? Python 2-为什么“ with”在嵌入式C代码中表现不同? - python 2 - why does 'with' behave differently in embedded c code? 为什么Python **运算符在数组和标量上的行为不同 - Why does the Python ** operator behave differently on arrays and scalars 为什么 Python 对相同的函数(例如“sum”或“and”)表现不同? - Why does Python behave differently for the same function such as 'sum' or 'and'? Python:为什么带有附加参数的方法的行为有所不同? - Python: Why Does a Method Behave Differently with an Added Parameter? 为什么RestrictedPython在与Python 3.6一起使用时表现不同? - Why does RestrictedPython behave differently when used with Python 3.6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM