python3.x中的arr [:] = []和arr [] = []之间的区别? [英] Difference between arr[:]=[] and arr[]=[] in python3.x?
本文介绍了python3.x中的arr [:] = []和arr [] = []之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这两行之间是否有任何区别:
Is there any difference between these two lines:
arr[:] = []
arr = []
我知道他们两个都清楚了.
I know both of them clear list.
推荐答案
对于第二个问题,我认为您的意思是 arr = []
.
For the second one, I think you meant arr = []
.
不同之处在于,它具有 arr 指向 new 空列表,只是减少了现有列表上的引用计数.
What that does differently is that it has arr point to a new empty list and just decrements the refcount on the existing list.
仅当其他内容指向原始列表时,差异才重要.
The difference is only important if something else is pointing to the original list.
>>> orig = [10, 20, 30]
>>> arr = orig # Second reference to the same list.
>>> arr[:] = [] # Clears the one list, so that arr and orig are empty
>>> orig
[]
与之对比:
>>> orig = [10, 20, 30]
>>> arr = orig # Second reference to the same list.
>>> arr = [] # Points arr to a new list, leaving orig unchanged
>>> orig
[10, 20, 30]
这篇关于python3.x中的arr [:] = []和arr [] = []之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文