list.append(another_list) vs list.append(another_list[:]) 在 python 中?
list.append(another_list) vs list.append(another_list[:]) in python?
这是我必须做的一道题:
我们将实现一个非常有用的功能,称为组。
group 采用事物列表和 returns 组列表,其中每个组由列表中所有相等的连续元素组成。
例如:
组([1, 1, 1, 2, 3, 1, 1]) == [[1, 1, 1], [2], [3], [1, 1]]
组([1, 2, 1, 2, 3, 3]) == [[1], [2], [1], [2], [3, 3]]
这是我的初步解决方案:
def group(int_list):
group_list = []
current_list = []
for i in range(len(int_list)):
if int_list[i] not in current_list:
if len(current_list) != 0:
group_list.append(current_list)
del current_list[:]
current_list.append(int_list[i])
else:
current_list.append(int_list[i])
group_list.append(current_list)
return group_list
我得到的输出:
[[1, 1], [1, 1], [1, 1], [1, 1]]
在花了大约 30 分钟试图找出问题后,我将第 9 行从 group_list.append(current_list)
更改为 group_list.append(current_list[:])
并且令人惊讶的是魔法起作用了。我得到了正确的输出:
[[1, 1, 1], [2], [3], [1, 1]]
所以我想我的问题是 current_list
和 current_list[:]
之间有什么区别?
current_list[:]
是 current_list
的 浅拷贝 ;例如:
- Introducing lists: Slicing shorthand
- Python list slice syntax used for no obvious reason
- "copying a list the right way"
在您的函数中,您正在 current_list
引用的事物中建立(当前组的)列表。完成后,将此内容添加到 group_list
,然后通过删除其所有内容 (del current_list[:]
) 来重置此内容。我们必须记住 Python 中的所有内容都是引用,因此,使用您的第一个代码,group_list
包含对 相同对象 的多个引用(这就是为什么您的输出看起来像 [[1, 1], [1, 1], [1, 1], [1, 1]]
)。当您删除 current_list
的内容并稍后添加新元素时,您也会对 group_list
的每个元素执行此操作。
使用您发现的 current_list[:]
语法,您创建了 current_list
的副本并将其添加到 group_list
;当您删除 current_list
的内容时,此副本不会被修改。
基本上主要区别在于 current_list 是对列表的引用,而 current_list[:] 是一个包含列表元素的新数组。因此,使用第一个,当您更改 current_list 时,group_list 也会更改。另一种方式,如果您更改 current_list,则 group_list 不会被修改。
这是我必须做的一道题:
我们将实现一个非常有用的功能,称为组。
group 采用事物列表和 returns 组列表,其中每个组由列表中所有相等的连续元素组成。
例如:
组([1, 1, 1, 2, 3, 1, 1]) == [[1, 1, 1], [2], [3], [1, 1]]
组([1, 2, 1, 2, 3, 3]) == [[1], [2], [1], [2], [3, 3]]
这是我的初步解决方案:
def group(int_list):
group_list = []
current_list = []
for i in range(len(int_list)):
if int_list[i] not in current_list:
if len(current_list) != 0:
group_list.append(current_list)
del current_list[:]
current_list.append(int_list[i])
else:
current_list.append(int_list[i])
group_list.append(current_list)
return group_list
我得到的输出:
[[1, 1], [1, 1], [1, 1], [1, 1]]
在花了大约 30 分钟试图找出问题后,我将第 9 行从 group_list.append(current_list)
更改为 group_list.append(current_list[:])
并且令人惊讶的是魔法起作用了。我得到了正确的输出:
[[1, 1, 1], [2], [3], [1, 1]]
所以我想我的问题是 current_list
和 current_list[:]
之间有什么区别?
current_list[:]
是 current_list
的 浅拷贝 ;例如:
- Introducing lists: Slicing shorthand
- Python list slice syntax used for no obvious reason
- "copying a list the right way"
在您的函数中,您正在 current_list
引用的事物中建立(当前组的)列表。完成后,将此内容添加到 group_list
,然后通过删除其所有内容 (del current_list[:]
) 来重置此内容。我们必须记住 Python 中的所有内容都是引用,因此,使用您的第一个代码,group_list
包含对 相同对象 的多个引用(这就是为什么您的输出看起来像 [[1, 1], [1, 1], [1, 1], [1, 1]]
)。当您删除 current_list
的内容并稍后添加新元素时,您也会对 group_list
的每个元素执行此操作。
使用您发现的 current_list[:]
语法,您创建了 current_list
的副本并将其添加到 group_list
;当您删除 current_list
的内容时,此副本不会被修改。
基本上主要区别在于 current_list 是对列表的引用,而 current_list[:] 是一个包含列表元素的新数组。因此,使用第一个,当您更改 current_list 时,group_list 也会更改。另一种方式,如果您更改 current_list,则 group_list 不会被修改。