Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts

    Python piecewise addition of list/tuple elements

    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?

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by ghell View Post
    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.

  • #3
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    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.

  • #4
    New Coder
    Join Date
    May 2006
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    I am not sure if they have the features you want, but you can also check out NumPy or PyMat (interface to Matlab).
    Last edited by ghostdog74; 06-19-2007 at 03:41 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •