PDA

View Full Version : Python piecewise addition of list/tuple elements



ghell
06-18-2007, 08:36 PM
I was wondering if there was any shorthand to do something like (1,2) + (3,4) * 2 = (7,10) or you have to actually loop through everything (if you dont know how big the thing is)

Matlab has something like .+ I believe, is there anything in python?

jkd
06-18-2007, 11:37 PM
Matlab has something like .+ I believe, is there anything in python?

+ and - are by default vectorized actually, you are thinking of .*, ./, and .^. :)

As for Python, you could always define a Vector class and implement __add__ on it to abstract the looping aspect.

ghell
06-19-2007, 01:08 AM
Yea I know I could do that or a lot of other things, i was just hoping to be lazy.

Not had much experience in matlab, I just knew you could stick . on some things to make it do it to each of the elements of a matrix.

Thanks for the help anyway.

ghostdog74
06-19-2007, 01:16 AM
I am not sure if they have the features you want, but you can also check out NumPy or PyMat (interface to Matlab).