[英]Python 2.7.2 Win32 # Loop Through List, Buffer Part Of List, Call Function Multiple Times, SAME Return
I have coded up some small Python script that calls a function multiple times. 我已经编写了一些小的Python脚本,该脚本多次调用一个函数。 The function should always return other values, because the buffer is always unique (due to .pop(0)
). 该函数应始终返回其他值,因为该缓冲区始终是唯一的(由于.pop(0)
)。 But the return is always the same. 但是回报总是一样的。 If I call the function two times like result1 = x(buffer1)
and result2 = x(buffer2)
outside of the loop, the results are unique. 如果我在循环外部两次调用函数result1 = x(buffer1)
和result2 = x(buffer2)
,则结果是唯一的。 But as soon as I try to call the function like in the Pseudo- Code, the result is always the same. 但是,一旦我尝试像伪代码中那样调用函数,结果总是相同的。 It seems as if Python is executing the function only once, and then printing the result of it. 似乎Python仅执行一次该函数,然后打印其结果。 I have implemented this code in PHP
and VB.net
, and it works as it should. 我已经在PHP
和VB.net
实现了此代码,它可以正常工作。 It seems to be a specific Python thing. 这似乎是特定于Python的事情。 Can somebody please explain. 有人可以解释一下。 :/ :/
Pseudo- Code 伪代码
function x()
list = []
buffer = []
for l in list:
buffer.append(l)
if len(buffer) == y:
return = x(buffer)
buffer.pop(0)
Python- Code Python代码
ema_length = {66:0,74:0,89:0}
def ema(length):
relevant = len(length)
multiplier = 2 / (relevant + 1)
sum_vector = 0
for vector in length:
sum_vector += vector
current_vector = vector
sma = sum_vector / relevant
if ema_length [relevant] == 0:
ema_length [relevant] = sma
else:
ema_length [relevant] = (current_vector - ema_length [relevant]) * multiplier + ema_length [relevant]
return ema_length [relevant]
buffer = []
for d in data:
buffer.append(d)
if len(buffer) == 89:
ema66 = ema(buffer [89 - 66:89]) # should be unique
ema74 = ema(buffer [89 - 74:89]) # should be unique
ema89 = ema(buffer [89 - 89:89]) # should be unique
ema_overall = ema66 - ema74 - ema89
buffer.pop(0)
You are passing your buffer to the function, but are not using it.. Have it like this. 您正在将缓冲区传递给该函数,但没有使用它。 :- :-
def x(old_buffer):
list = []
buffer = old_buffer
for l in list:
buffer.append(l)
if len(buffer) == y:
return = x(buffer)
buffer.pop(0)
We define function using def
not function
... 我们使用def
not function
定义function
...
BTW, what is your purpose of using this -> return = x(buffer)
way of return. 顺便说一句,使用此-> return = x(buffer)
返回方式的目的是什么。 ?? ?? What is that =
doing in between?? 那是什么=
在两者之间做?
Also, I don't see anything added to the list
before using it.. Are you pasing any list
into that function
?? 另外,在使用它之前,我看不到任何添加到list
内容。.您是否将任何list
粘贴到该function
? Else you are re-creating that too on every call.. 否则,您也会在每个电话上重新创建该文件。
I think this is the problem: 我认为这是问题所在:
def ema(length):
# ...
for vector in length:
sum_vector += vector
current_vector = vector
sma = sum_vector / relevant
if ema_length [relevant] == 0:
ema_length [relevant] = sma
else:
ema_length [relevant] = (current_vector - ema_length [relevant]) * multiplier + ema_length [relevant]
return ema_length [relevant]
The function ema
gets a list of values from your buffer. 函数ema
从缓冲区中获取值列表。 The last element is always the same (as you always call it with buffer[89 - X:89]
). 最后一个元素总是相同的(就像您总是使用buffer[89 - X:89]
调用它一样)。 Now, you loop through the elements, sum them up and set current_vector
to the current one. 现在,您遍历元素,对其求和,并将current_vector
设置为当前元素。 After the loop finished, current_vector
is set to the last one, buffer[89]
—always. 循环结束后, current_vector
始终设置为最后一个,即buffer[89]
。 Afterwards you use current_vector
to set up the return value. 然后,使用current_vector
设置返回值。 So the problem is that you exit the loop to early. 因此,问题在于您要尽早退出循环。
multiplier = 2 / (relevant + 1) # floats are rounded! => * with 0 => same result
multiplier = float(2) / float(relevant + 1) # unique values! :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.