I have coded up some small Python script that calls a function multiple times. The function should always return other values, because the buffer is always unique (due to .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. 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. I have implemented this code in PHP
and VB.net
, and it works as it should. It seems to be a specific Python thing. 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
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
...
BTW, what is your purpose of using this -> return = x(buffer)
way of return. ?? 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
?? 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. The last element is always the same (as you always call it with buffer[89 - X:89]
). Now, you loop through the elements, sum them up and set current_vector
to the current one. After the loop finished, current_vector
is set to the last one, buffer[89]
—always. Afterwards you use current_vector
to set up the return value. 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! :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.