0

I am creating a newArrayList. Each of signalxOP array minus with gradient and multiple with each of appliedFieldOP array. (SignalxOP[] - Gradient) * appliedFieldOP[]

How can i do that?? Below is the method i tried. It keep giving me error.

signalxOP = [5,4,3,2,1]
appliedFieldOP= [1,0.5,0,-0.5,-1]
Gradient = 1.5
newList = [[(x-Gradient)*y] for x,y in signalxOP,appliedFieldOP]
py.plot(appliedFieldOP,newList)
py.show()

Error show

newList = [[(x-Gradient)*y] for x,y in signalxOP,appliedFieldOP]
ValueError: too many values to unpack
asked Dec 20, 2013 at 1:29

1 Answer 1

3

You can use zip function, like this

newList = [[(x-Gradient)*y] for x,y in zip(signalxOP,appliedFieldOP)]
answered Dec 20, 2013 at 1:31
Sign up to request clarification or add additional context in comments.

4 Comments

One probably should use izip in situations like that. However, this doesn't really matter for short lists.
@Suor: I'd go farther and say zip is better when you know they're short. Going out of your way to use izip implies that you're expecting potentially-large data (and that you're willing to gratuitously break 3.x compatibility, possibly scare off a few novices, and make your code a little slower on small data to do so), which is misleading when you're sure that you have small lists.
@abarnert izip is still faster, even for short lists. Try timeit [x + y for x, y in zip(l, l)] in ipython and same with izip.
@Suor: I don't know what your l is, but when I try it with l=[1,2] (Apple 64-bit CPython 2.7.5) it's 776ns for izip and 639ns for zip. So, that's slower, not faster. But more importantly, you're focusing on the least important part of a parenthetical aside and ignoring the actual point.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.