List.append/extend + 运算符和 +=
List.append/extend + operators and +=
我很难理解下面这段代码的行为。在 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_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 和增量运算符。输出:
# .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]
请注意,在所有这些示例中,当我获得二维数组(我需要)时。 a_list 中的值在操作 another_list 时发生变化。我如何获得执行此操作的代码?
#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_list.append(self.another_list.copy())
创建 another_list
的快照,然后将其添加到 a_list
。您的代码实际上将 another_list
添加为 a_list
的元素,因此以后的编辑很自然会更改该对象的内容。
如果您希望 a_list
保留为 [[0]]
而不管 another)list
中的第一个值发生什么变化,为什么不直接将其初始化为 [[0]]
中的 [=] 15=]?
def __init__(self):
self.a_list = [[0]]
self.another_list = [0]
# End of __init__; nothing else
使用 append
,您添加 another_list
的引用作为 a_list
的第一个元素。使用 extend
,您将 another_list
的元素的引用添加到 a_list
.
我很难理解下面这段代码的行为。在 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_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 和增量运算符。输出:
# .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]
请注意,在所有这些示例中,当我获得二维数组(我需要)时。 a_list 中的值在操作 another_list 时发生变化。我如何获得执行此操作的代码?
#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_list.append(self.another_list.copy())
创建 another_list
的快照,然后将其添加到 a_list
。您的代码实际上将 another_list
添加为 a_list
的元素,因此以后的编辑很自然会更改该对象的内容。
如果您希望 a_list
保留为 [[0]]
而不管 another)list
中的第一个值发生什么变化,为什么不直接将其初始化为 [[0]]
中的 [=] 15=]?
def __init__(self):
self.a_list = [[0]]
self.another_list = [0]
# End of __init__; nothing else
使用 append
,您添加 another_list
的引用作为 a_list
的第一个元素。使用 extend
,您将 another_list
的元素的引用添加到 a_list
.