Python List.append/extend+;运算符和+=
我很难理解下面代码的行为。在python 3.6中Python List.append/extend+;运算符和+=,python,list,append,extend,Python,List,Append,Extend,我很难理解下面代码的行为。在python 3.6中 下面的示例代码是我实际代码的抽象。我这样做是为了更好地描述我的问题。我正在尝试将一个列表添加到另一个列表中。产生一个二维列表。为了检查成员资格,请在以后查看该列表。虽然我无法以我喜欢的方式添加我的列表 a_list = [] another_list = [7,2,1] a_list.DONT_KNOW(another_list) another_list = [7,3,1] 结果: a_list [[7,2,1]] another_lis
下面的示例代码是我实际代码的抽象。我这样做是为了更好地描述我的问题。我正在尝试将一个列表添加到另一个列表中。产生一个二维列表。为了检查成员资格,请在以后查看该列表。虽然我无法以我喜欢的方式添加我的列表
a_list = []
another_list = [7,2,1]
a_list.DONT_KNOW(another_list)
another_list = [7,3,1]
结果:
a_list
[[7,2,1]]
another_list
[7,3,1]
class foo:
def __init__(self):
self.a_list = []
self.another_list = [0]
####### Modifying .extend/.append##############
self.a_list.append(self.another_list) # .append() | .extend(). | extend([])
###############################################
def bar(self):
######## Modifying operator########
self.another_list[0] += 1 # += | +
###################################
print('a_list = {} another_list = {} '.format(self.a_list, self.another_list))
def call_bar(f, repeats):
x = repeats
while x > 0:
x -= 1
foo.bar(f)
f = foo()
call_bar(f,3)
我的问题示例:
a_list
[[7,2,1]]
another_list
[7,3,1]
class foo:
def __init__(self):
self.a_list = []
self.another_list = [0]
####### Modifying .extend/.append##############
self.a_list.append(self.another_list) # .append() | .extend(). | extend([])
###############################################
def bar(self):
######## Modifying operator########
self.another_list[0] += 1 # += | +
###################################
print('a_list = {} another_list = {} '.format(self.a_list, self.another_list))
def call_bar(f, repeats):
x = repeats
while x > 0:
x -= 1
foo.bar(f)
f = foo()
call_bar(f,3)
重复5次。修改list.function和increment运算符。输出:
# .append() and +=
a_list = [[1]] another_list = [1]
a_list = [[2]] another_list = [2]
a_list = [[3]] another_list = [3]
# .extend() and +=
a_list = [0] another_list = [1]
a_list = [0] another_list = [2]
a_list = [0] another_list = [3]
# .append() and +
a_list = [[1]] another_list = [1]
a_list = [[2]] another_list = [2]
a_list = [[3]] another_list = [3]
#.extend() and +
a_list = [0] another_list = [1]
a_list = [0] another_list = [2]
a_list = [0] another_list = [3]
#.extend([]) and +
a_list = [[1]] another_list = [1]
a_list = [[2]] another_list = [2]
a_list = [[3]] another_list = [3]
注意,在所有这些示例中,当我得到二维数组时(我需要)。操作另一个\u列表时,一个\u列表中的值会发生变化。我如何获得执行此操作的代码
#SOME METHOD I DON'T KNOW
a_list = [[0]] another_list = [1]
a_list = [[0]] another_list = [2]
a_list = [[0]] another_list = [3]
您必须使用
self.a\u list.append(self.other\u list.copy())
来创建另一个\u list
的快照,然后将其添加到a\u list
。您的代码实际上添加了另一个列表作为列表的元素,因此以后的编辑自然会更改该对象的内容 您必须使用self.a\u list.append(self.another\u list.copy())
创建另一个\u list
的快照,然后将其添加到a\u list
。您的代码实际上添加了另一个列表作为列表的元素,因此以后的编辑自然会更改该对象的内容 如果希望a_列表
保持为[[0]]
状态,而不管另一个列表
中的第一个值发生了什么变化,为什么不在\uu init
中将其初始化为[[0]]
def __init__(self):
self.a_list = [[0]]
self.another_list = [0]
# End of __init__; nothing else
使用
append
,您可以添加一个另一个\u列表
的引用,作为a\u列表
的第一个元素。使用extend
,您可以将另一个
列表的元素的引用添加到另一个
列表中,如果您希望另一个
列表保持为[[0]
状态,而不管另一个)列表中的第一个值发生了什么,为什么不在初始化中将其初始化为[[0]
def __init__(self):
self.a_list = [[0]]
self.another_list = [0]
# End of __init__; nothing else
使用append
,您可以添加一个另一个\u列表
的引用,作为a\u列表
的第一个元素。使用extend
,您可以将另一个列表的元素的引用添加到一个列表中。使用切片时必须返回一份副本?是的,它会复制列表中的每个条目。有些人认为复制列表是习惯用法,有些人认为它很难看。AyList.AppEnter(AddiaList[]:)确实有效。使用切片时必须返回一份副本?是的,它会复制列表中的每个条目。有些人认为这是抄袭列表的惯用方式,有些人认为它很难看。谢谢你,伙计。有道理,但不是很明显。\n太棒了!谢谢你,伙计。这是有道理的,虽然不是很明显。\n不完全是我想要实现的。虽然对append和extend的解释很受欢迎:)Np,但是你想实现什么呢?B/c我的答案产生的结果与您标记为正确的结果相同,但它避免调用copy()
“我正试图将一个列表添加到另一个列表中。结果是一个二维列表。为了检查成员资格,请稍后查看该列表。”这与我试图实现的不完全相同。虽然对append和extend的解释很受欢迎:)Np,但是你想实现什么呢?B/c我的答案产生的结果与您标记为正确的结果相同,但它避免调用copy()
“我试图将一个列表添加到另一个列表中。结果是一个二维列表。为了检查该列表的成员资格,请稍后再调用该列表。”