简体   繁体   English

python打印列表中的混乱

[英]confusion in python printing the lists

In the following python code, I have a list ie to_do_list of two lists ie other_events and grocery_list where I inserted an item in grocery_list and then deleted it. 在下面的python代码中,我有一个列表,即to_do_list和两个列表,即other_eventsgrocery_list列表,在其中我将grocery_list插入了grocery_list ,然后将其删除。

My confusion is, when I updated grocery_list , to_do_list is automatically updated but when I deleted grocery_list , to_do_list is not updated... why? 我的困惑是,当我更新grocery_listto_do_list会自动更新,但是当我删除grocery_listto_do_list却不会更新...为什么?

grocery_list = ['banana', 'mango', 'apple']

other_events =['pick up kids','do laundry', 'dance class']

to_do_list = [other_events, grocery_list]

print(to_do_list)

grocery_list.insert(1,'onions');

print(grocery_list)

del grocery_list

print(to_do_list)

its output is: 其输出为:

[['pick up kids', 'do laundry', 'dance class'], ['banana', 'mango', 'apple']]
['banana', 'onions', 'mango', 'apple']
[['pick up kids', 'do laundry', 'dance class'], ['banana', 'onions', 'mango', 'apple']]

use del grocery_list[:] instead ouput : 使用del grocery_list[:]代替ouput:

[['pick up kids', 'do laundry', 'dance class'], ['banana', 'mango', 'apple']]
['banana', 'onions', 'mango', 'apple']
[['pick up kids', 'do laundry', 'dance class'], []]

but why ? 但是为什么呢?
assume we have a list : 假设我们有一个清单:
grocery_list = ['banana', 'mango', 'apple']

del grocery_list
grocery_list

when you use this to delete list program raise an error : 当您使用它删除列表程序时引发错误:

Traceback (most recent call last):
  File "<pyshell#619>", line 1, in <module>
    del grocery_list
NameError: name 'grocery_list' is not defined

in this code you used del grocery_list which only delete grocery_list ( Deletion of a name removes the binding of that name from the local or global namespace ) and don't clear the list you must use del grocery_list[:] to compeletely delete it for more detail for del ref to this 在此代码中,您使用了del grocery_list ,该删除仅删除了grocery_list删除名称会从本地或全局命名空间中删除该名称的绑定 ),并且不清除列表,您必须使用del grocery_list[:]来彻底删除它详细的del裁判这个

The reason why, when you update grocery_list your object to_do_list is updated is that your list to_do_list contain a only reference on grocery_list . 为什么,当你更新的原因grocery_list你的对象to_do_list更新是你的名单to_do_list包含一个唯一的参考 grocery_list

Now, why after deleting grocery_list , to_do_list is not updated is that the keywork del doesn't delete the object on which but only delete grocery_list from the namespace. 现在,为什么在删除grocery_list to_do_list后, to_do_list不会更新,是因为键值del不删除其上的对象,而只是从命名空间中删除grocery_list

Here is a quote from the Python Language Reference on what del does : 这是《 Python语言参考 》中del作用的引文:

Deletion of a name removes the binding of that name from the local or global namespace 删除名称会从本地或全局名称空间中删除该名称的绑定

grocery_list has many references to it and the grocery_list对此有很多引用,

del grocery_list

will only remove one reference while other references will exists. 将仅删除一个参考,而其他参考将存在。

One way to remove all the references from a list is to use slice assignment 从列表中删除所有引用的一种方法是使用切片分配

del grocery_list[:]

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

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