对于此列表:
current_trace = [[3,5,1,5,7,9,4]]
我运行sliding_tristep()
方法,其中包括predict()
和window()
方法:
def predict(lst):
print "predicting for", lst
print "result", max(lst) + 0.0
return max(lst) + 0.0
def window(lst, n=3):
for x in range(1, len(lst)+1): # or len(l)+n to continue till the end
yield(lst[max(0, x-n):x])
def sliding_tristep(full_trace, future_step = 2, window_size = 3):
for user_trace in full_trace:
for current_input in window(user_trace):
counter = 0
trace = current_input
accumulator = []
while counter <= future_step:
next_prediction = predict(trace)
trace.append(next_prediction)
accumulator.append(next_prediction)
trace = trace[-window_size:]
counter += 1
print current_input, accumulator
当我运行sliding_tristep(current_trace)
时,在print current_input, accumulator
行的输出中,我注意到current_input
已经被修改,尽管它不在while循环中,while循环在sliding_tristep(current_trace)
中进行计算。你知道吗
我想知道为什么会这样?python怎么可能修改一个在后续循环中根本没有使用的列表。你知道吗
刚刚测试了你的代码:
current_trace
没有被修改。你知道吗不过,我猜你的意思是
current_input
,而不是current_trace
。你知道吗current_input
被修改,因为trace
是对current_input
的引用,跟踪被修改。你知道吗如果要将
current_input
复制为trace
,有一种方法:应用于您的代码:
如果不修改列表中的元素(由于整数是不可变的,所以不能修改),可以按照我在前面的示例中建议的那样进行浅层复制。如果您使用的是可变对象(
lists
或其他类型的对象),那么您需要使用copy
模块进行深度复制。看看这个答案:https://stackoverflow.com/a/184660/1290438关于这个话题。你知道吗使用
trace.extend(current_input)
而不是trace = current_input
解决了问题。不过,必须事先初始化trace
列表。你知道吗解决方案如下:
相关问题 更多 >
编程相关推荐