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
user2837162
1 Answer 1
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
thefourtheye
241k53 gold badges466 silver badges505 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
abarnert
@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.Suor
@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.abarnert
@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.lang-py