Invalid syntax using dict definition

Given a list of floats with the name 'x', I would like to create a dictation for each x in x [1: -1] for its neighbors using a dict understanding. I tried the following line:

neighbours = {x1:(x0,x2) for (x0,x1,x2) in zip(x[:-2],x[1:-1],x[2:])}

However, the syntax seems invalid. What am I doing wrong?

+5
source share
1 answer

Understanding Dict is only available in Python 2.7 up. For earlier versions, you need a constructor dict()with a generator:

dict((x1, (x0,x2)) for (x0,x1,x2) in zip(x[:-2],x[1:-1],x[2:]))
+19
source

All Articles