Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python列表附加_Python_List_Append - Fatal编程技术网

Python列表附加

Python列表附加,python,list,append,Python,List,Append,我想在Python中存储变量的中间值。此变量在循环中更新。当我尝试使用list.append命令执行此操作时,它会使用变量的新值更新列表中的每个值。我该怎么做 while (step < maxstep): for i in range(100): x = a*b*c f1 += x f2.append(f1) print f2 raw_input('<<') step += 1 while(步骤

我想在Python中存储变量的中间值。此变量在循环中更新。当我尝试使用
list.append
命令执行此操作时,它会使用变量的新值更新列表中的每个值。我该怎么做

while (step < maxstep):
    for i in range(100):    
        x = a*b*c
        f1 += x
    f2.append(f1)
    print f2
    raw_input('<<')
    step += 1
while(步骤原始输入('看起来您正在将同一数组追加到列表中,然后更改数组的内容

每次将数组对象附加到
f2


如果您需要更多帮助,您需要在问题中的代码中添加更多信息。目前这没有多大意义(《代码》fi
的值在哪里更改?

假设原文有一个打字错误,并且
f1
实际上是
fi
(反之亦然):

fi
是指向某个对象的指针,因此当您
fi+=x
实际更改了
fi
指向的对象的值时,您会一直附加同一个指针。希望这是清楚的


要解决这个问题,你可以用fi=fi+x来代替。

我想你的意思是这样的:

   f2 = []
   f1 = 0
   for i in range(100):    
       x = f()
       f1 += x
   f2.append(f1)
   print f2

请注意,如果
f1
是一个可变对象,则行
f1+=x
不会创建新对象,而只会更改
f1
的值,因此它在
f2
数组中的所有引用都会被更新。

您确实应该粘贴一个工作示例

您要追加的对象(fi)是可变的(),这意味着,本质上,您追加的是对对象的引用,而不是对象值。因此,列表索引0和1实际上是同一个对象


您需要在每次循环迭代中创建一个新对象(
fi=array()
),或者使用。

另一个相关问题是“”。 Daren Thomas使用赋值来解释变量传递在python中的工作原理。对于append方法,我们可以用类似的方式来思考。假设您将一个列表“值列表”附加到一个列表“变量列表”

追加操作可以被认为是:

list_of_variables[0] = list_of_values --> [1, 2, 3]
list_of_values --> [1, 2, 3, 10]
list_of_variables[1] = list_of_values --> [1, 2, 3, 10]
由于“变量列表”中的第一项和第二项指向内存中的同一对象,因此上面的输出为:

List of variabiles after 1st appending:  [[1, 2, 3]]
List of variables after 2nd appending:  [[1, 2, 3, 10], [1, 2, 3, 10]]

另一方面,如果“值列表”是一个变量,则行为会有所不同

list_of_variables = []
variable = 3
list_of_variables.append(variable)
print "List of variabiles after 1st appending: ", list_of_variables
variable = 10
list_of_variables.append(variable)
print "List of variables after 2nd appending: ", list_of_variables
现在的追加操作相当于:

list_of_variables[0] = variable --> 3
variable --> 4
list_of_variables[1] = variable --> 4
输出为:

List of variabiles after 1st appending:  [3]
List of variables after 2nd appending:  [3, 10]

变量值和列表值之间的差异是后一个值发生了变化。

条件
m
的含义是什么?什么是
fi
?什么是
f()
?X是基于函数更新的,m是for循环的范围,while循环运行任意数量的步骤,但现在我很困惑,我看到您有
f1
fi
这是您代码中的一个输入错误吗?这是一个输入错误:)thanksAh!,我没有意识到f1+=x和f1=f1+x之间有这么大的区别,它解决了这个问题:)为下一代添加了您的解决方案
list_of_variables = []
variable = 3
list_of_variables.append(variable)
print "List of variabiles after 1st appending: ", list_of_variables
variable = 10
list_of_variables.append(variable)
print "List of variables after 2nd appending: ", list_of_variables
list_of_variables[0] = variable --> 3
variable --> 4
list_of_variables[1] = variable --> 4
List of variabiles after 1st appending:  [3]
List of variables after 2nd appending:  [3, 10]