print reduce SyntaxError: invalid syntax

python 3 reduce
python reduce initial value
python reduce dictionary
python reduce(lambda)
python 3 reduce not defined
python filter
python map
how to fix invalid syntax in python

When I call this method patternFinder() I got a error like this

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 print reduce((lambda x,y:x+y,outcomeRange)/len(outcomeRange)) 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. 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:

from __future__ import division

Intro Python Debugging, not match any outer indention level unexpected indent. Solution: When you press OK on the dialog box. yongtang changed the title tf.print throw out SyntaxError: invalid syntax tf.print usage throws SyntaxError: invalid syntax Oct 13, 2019 ravikyram self-assigned this Oct 14, 2019 ravikyram added comp:ops TF 2.0 type:support labels Oct 14, 2019

reduce takes two separate arguments, function and iterable. 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. Otherwise, convert the division to [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)

Invalid Syntax in Python: Common Reasons for SyntaxError – Real , (Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by The syntax of the reduce() function is as follows​: Syntax: reduce(function print(reduce(do_sum, [1, 2, 3, 4])). 7. ​  Dan, Thanks a lot for reviewing my code! I can't believe I didn't notice these mistakes. Everything works as it should now. Brian

You are most likely using Python 3+, which means that the syntax of your print statement is incorrect. 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.

Difference between NameError and SyntaxError, 1 # theofficefacts.py 2 ages = { 3 'pam': 24, 4 'jim': 24 5 'michael': 43 6 } 7 print(f'​Michael is {ages["michael"]} years old.') You can see the invalid  The message reads SyntaxError: invalid syntax, but that’s not very helpful. The traceback points to the first place where Python could detect that something was wrong. To fix this sort of error, make sure that all of your Python keywords are spelled correctly.

Python reduce() function, Chapter on the Lambda Operator and the functions map, filter and reduce. The general syntax of a lambda function is quite simple: C = [39.2, 36.5, 37.3, 38, 37.8] >>> F = list(map(lambda x: (float(9)/5)*x + 32, C)) >>> print(F) [102.56, 97.7,​  $ frosted print2.py print2.py:3:21: invalid syntax print " Hello, Frosted " ^ $ frosted print3.py $ frosted printbroken.py printbroken.py:3:23: invalid syntax this is not valid Python for any version of the language ^

Python Tutorial: Lambda Operator, filter, reduce and map, In Python 3, print is an expression and it works like you'd expect: [code] File "<​stdin>", line 1; print "hello world"; ^; SyntaxError: invalid syntax; >>> print("hello worl. There actually are simple steps to dramatically reduce online tracking. Stack Exchange Network. Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Why does putting print inside a Python lambda function raise a , Regular Languages and Finite Automata · Context Free Grammar and Context Free Languages · Turing Machine Calendar Functions in Python | Set 1( calendar(), month(), isleap()…) print (functools. reduce ( lambda a,b : a if a > b else b,lis)) Please write comments if you find anything incorrect, or you want to share  File "question.py", line 5 print ','.join(l) ^ SyntaxError: invalid syntax

Comments
  • You are using Python 2 syntax with Python 3 if I had to guess. Also you are dividing inside the reduce and not outside.
  • which python version are you using? Python 3 or Python 2? if python 3, the print statement needs parenthesis
  • Pretty sure the division needs to be out of the reduce if anything.
  • @kabanus: it not part of the reduce, it's part of the expression producing the iterable.
  • I think OP is calculating an average, besides, if outcomeRange is not a numpy array that division is a syntax error.
  • @kabanus: I have already written that it's a syntax error if it's not numpy, and provided alternative syntax
  • @kabanus: the OP has mixed mixed up parentheses, so it's impossible to know what the exact order of the operations should be.