[英]print reduce SyntaxError: invalid syntax
When I call this method patternFinder() I got a error like this当我调用这个方法 patternFinder() 时,我收到了这样的错误
def patternFinder():
avgaLine=((bid+ask)/2)
x=len(avgaLine)-30
y=11
while y<x:
outcomeRange=avgaLine[y+20:y+30]
currentPoint=avgaLine[y]
print reduce((lambda x,y:x+y,outcomeRange)/len(outcomeRange))
print (currentPoint)
print ('_________')
time.sleep(5555)
I get this error:我收到此错误:
File
<ipython-input-66-a32566f8689e>
, line 21文件<ipython-input-66-a32566f8689e>
,第 21 行
print reduce((lambda x,y:x+y,outcomeRange)/len(outcomeRange))打印减少((λ x,y:x + y,结果范围)/ len(结果范围))
SyntaxError: invalid syntax语法错误:无效语法
That's not the correct way to use reduce
(the division should be done after the reduction), and besides you don't even need to use reduce
in here.这并不是正确的使用方法reduce
(师应减少后进行),此外你甚至不需要使用reduce
在这里。 If you want to calculate an average, try this instead:如果你想计算平均值,试试这个:
print( sum(outcomeRange) / len(outcomeRange) )
By the way, beware of integer division.顺便提一下,注意整数除法。 Make sure that outcomeRange
contains at least one decimal value, or that you imported the current division function:确保outcomeRange
至少包含一个十进制值,或者您导入了当前除法函数:
from __future__ import division
reduce
takes two separate arguments, function and iterable. reduce
需要两个独立的参数,函数和可迭代的。 So, you line should be所以,你的线应该是
reduce(lambda x,y:x+y, outcomeRange/len(outcomeRange))
However, also make sure that your outcomeRange
is a numpy array, otherwise you'll get another syntax error for trying to divide it by a number.但是,还要确保您的outcomeRange
是一个 numpy 数组,否则您将在尝试除以数字时遇到另一个语法错误。 Otherwise, convert the division to [x/len(outcomeRange) for x in outcomeRange]
否则,将除法转换为[x/len(outcomeRange) for x in outcomeRange]
In that case, it would be better to divide by the lenght after the summation is done, so, you could write在这种情况下,最好在求和完成后除以长度,所以,你可以写
reduce(lambda x, y: x+y, outcomeRange) / len(outcomeRange)
You are most likely using Python 3+, which means that the syntax of your print
statement is incorrect.您最有可能使用 Python 3+,这意味着您的print
语句的语法不正确。 It needs to be in parentheses:它需要在括号中:
print(reduce((lambda x,y:x+y,outcomeRange)/len(outcomeRange)))
You've done it correctly in the other 2 print
statements, I guess you've just missed it here.您在其他 2 个print
语句中已正确完成,我想您只是在这里错过了它。
For Python 3.8
(Jupyter Notebook Anaconda)对于Python 3.8
(Jupyter Notebook Anaconda)
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
import numpy as np
import time
def bytespdate2num(b):
return mdates.datestr2num(b.decode('utf-8'))
date, bid, ask = np.loadtxt('GBPUSD1d.txt', unpack=True,
delimiter=',', converters={0:bytespdate2num})
def percentChange (startPoint, currentPoint):
return ((float(currentPoint)-startPoint)/startPoint)*100.0
def patternFinder():
avgLine = ((bid+ask)/2)
x = len(avgLine) - 30
y = 11
while y < x:
p1 = percentChange(avgLine[y-10], avgLine[y-9])
p2 = percentChange(avgLine[y-10], avgLine[y-8])
p3 = percentChange(avgLine[y-10], avgLine[y-7])
p4 = percentChange(avgLine[y-10], avgLine[y-6])
p5 = percentChange(avgLine[y-10], avgLine[y-5])
p6 = percentChange(avgLine[y-10], avgLine[y-4])
p7 = percentChange(avgLine[y-10], avgLine[y-3])
p8 = percentChange(avgLine[y-10], avgLine[y-2])
p9 = percentChange(avgLine[y-10], avgLine[y-1])
p10 = percentChange(avgLine[y-10], avgLine[y])
outcomeRange = avgLine[y+20:y+30]
currentPoint = avgLine[y]
print (sum(outcomeRange) / len(outcomeRange) )
print (currentPoint)
print (' ____')
print (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
y += 1
time.sleep(5555)
patternFinder()
Output输出
1.553601
1.553555
-0.001930961683282023 -0.0032182694721319405 -0.0032182694721319405 -0.0035400964193622853 -0.003861923366564046 -0.005471058102644308 -0.0061147119970621205 -0.00450557726099615 -0.00450557726099615 -0.004827404208212203
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.