简体   繁体   English

为什么列表没有变化?

[英]Why doesn't list change?

In Python:在 Python 中:

    b = []
    a = np.arange(2)
    b.append(a)
    a = np.arange(4)
    b.append(a)
    print(b) # gives [array([0, 1]), array([0, 1, 2, 3])]

Why doesn't it give [array([0, 1, 2, 3]), array([0, 1, 2, 3])] ?为什么不给出[array([0, 1, 2, 3]), array([0, 1, 2, 3])]

There are a ton of questions going the other way (to make sure the list does not change -- see here or this explanation on referencing and values ).有很多问题是相反的(以确保列表不会改变 - 请参阅此处有关引用和值的解释)。

From the links above I would expect b[0] == b[1] because I updated a , but this is not the case.从上面的链接中,我希望b[0] == b[1]因为我更新a ,但事实并非如此。 What am I missing?我错过了什么?

a = np.arange(4) doesn't update the values inside a , it reassigns the reference to a new array. a = np.arange(4)不会更新 a 中a值,它会将引用重新分配给新数组。 This is reflected in b , as it has two different arrays as elements.这反映在b中,因为它有两个不同的 arrays 作为元素。

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

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