How to efficiently proceed addition and subtraction in python list?

Discussion in 'Python' started by Daniel Mark, Sep 18, 2006.

  1. Daniel Mark

    Daniel Mark Guest

    Hello all:

    I have a list AAA = [1, 2, 3] and would like to subtract one from list
    AAA
    so AAA' = [0, 1, 2]

    What should I do?


    Thank you
    -Daniel
     
    Daniel Mark, Sep 18, 2006
    #1
    1. Advertising

  2. Daniel Mark

    Tim Chase Guest

    Re: How to efficiently proceed addition and subtraction in pythonlist?

    > I have a list AAA = [1, 2, 3] and would like to subtract one from list
    > AAA
    > so AAA' = [0, 1, 2]
    >
    > What should I do?



    Sounds like a list comprehension to me:

    >>> a = [1,2,3]
    >>> a_prime = [x-1 for x in a]
    >>> a_prime

    [0, 1, 2]

    -tkc
     
    Tim Chase, Sep 18, 2006
    #2
    1. Advertising

  3. Daniel Mark

    Chiucs Guest

    try this
    a=[1, 2, 3]
    b=a[:]

    "Daniel Mark" <> ???
    news: ???...
    > Hello all:
    >
    > I have a list AAA = [1, 2, 3] and would like to subtract one from list
    > AAA
    > so AAA' = [0, 1, 2]
    >
    > What should I do?
    >
    >
    > Thank you
    > -Daniel
    >
     
    Chiucs, Sep 19, 2006
    #3
  4. Daniel Mark

    Paul Rubin Guest

    "Daniel Mark" <> writes:
    > I have a list AAA = [1, 2, 3] and would like to subtract one from list
    > AAA
    > so AAA' = [0, 1, 2]
    >
    > What should I do?


    BBB = [x-1 for x in AAA]
     
    Paul Rubin, Sep 19, 2006
    #4
  5. Daniel Mark

    Ant Guest

    Tim Chase wrote:
    > > I have a list AAA = [1, 2, 3] and would like to subtract one from list
    > > AAA
    > > so AAA' = [0, 1, 2]
    > >
    > > What should I do?

    >
    >
    > Sounds like a list comprehension to me:


    Also the built in function 'map' would work:

    >>> a = [1,2,3]
    >>> b = map(lambda x: x-1, a)
    >>> b

    [0, 1, 2]

    List comprehensions are more pythonic, but map would probably be faster
    if performance was a (real) issue.
     
    Ant, Sep 19, 2006
    #5
  6. Daniel Mark

    Steve Holden Guest

    Re: How to efficiently proceed addition and subtraction in pythonlist?

    Ant wrote:
    > Tim Chase wrote:
    >
    >>>I have a list AAA = [1, 2, 3] and would like to subtract one from list
    >>>AAA
    >>>so AAA' = [0, 1, 2]
    >>>
    >>>What should I do?

    >>
    >>
    >>Sounds like a list comprehension to me:

    >
    >
    > Also the built in function 'map' would work:
    >
    >
    >>>>a = [1,2,3]
    >>>>b = map(lambda x: x-1, a)
    >>>>b

    >
    > [0, 1, 2]
    >
    > List comprehensions are more pythonic, but map would probably be faster
    > if performance was a (real) issue.
    >

    And statements like that are probably going to annoy me ;-)

    >>> t = timeit.Timer("b = map(lambda x: x-1, a)", setup="a=[1,2,3]")
    >>> t.timeit()

    2.4686168214116599
    >>> t = timeit.Timer("b = [x-1 for x in a]", setup="a=[1,2,3]")
    >>> t.timeit()

    0.9930245324475635
    >>>


    Any timing prediction involving the word "probably" isn't worth the
    paper it's written on (or even less if it's posted in a newsgroup ;-).
    If it's "probably" faster, and if performance is *really* important, you
    need to benchmark both options to remove the "probably". As the above
    test makes clear, your assertions are certainly untrue for 2.4.2 on Windows.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
     
    Steve Holden, Sep 19, 2006
    #6
  7. Re: How to efficiently proceed addition and subtraction in pythonlist?

    Steve Holden wrote:

    > And statements like that are probably going to annoy me ;-)
    >
    > >>> t = timeit.Timer("b = map(lambda x: x-1, a)", setup="a=[1,2,3]")
    > >>> t.timeit()

    > 2.4686168214116599
    > >>> t = timeit.Timer("b = [x-1 for x in a]", setup="a=[1,2,3]")
    > >>> t.timeit()

    > 0.9930245324475635
    > >>>


    And now someone's probably reading this and thinking that list comprehensions
    are *always* faster than "map"...

    </F>
     
    Fredrik Lundh, Sep 19, 2006
    #7
  8. Daniel Mark

    Steve Holden Guest

    Re: How to efficiently proceed addition and subtraction in pythonlist?

    Fredrik Lundh wrote:
    > Steve Holden wrote:
    >
    >
    >>And statements like that are probably going to annoy me ;-)
    >>
    >> >>> t = timeit.Timer("b = map(lambda x: x-1, a)", setup="a=[1,2,3]")
    >> >>> t.timeit()

    >>2.4686168214116599
    >> >>> t = timeit.Timer("b = [x-1 for x in a]", setup="a=[1,2,3]")
    >> >>> t.timeit()

    >>0.9930245324475635
    >> >>>

    >
    >
    > And now someone's probably reading this and thinking that list comprehensions
    > are *always* faster than "map"...
    >

    You are so right. So, let me repeat the important part that Fredrik
    didn't quote:

    """if performance is *really* important, you
    need to benchmark both options"""

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
     
    Steve Holden, Sep 19, 2006
    #8
  9. Daniel Mark

    Ant Guest

    Re: How to efficiently proceed addition and subtraction in pythonlist?

    Steve Holden wrote:
    > Fredrik Lundh wrote:
    > > Steve Holden wrote:
    > >

    ....
    > """if performance is *really* important, you
    > need to benchmark both options"""


    Fair point.
     
    Ant, Sep 19, 2006
    #9
  10. Re: How to efficiently proceed addition and subtraction in pythonlist?

    On 18 Sep 2006 15:43:31 -0700, Daniel Mark <> wrote:
    > Hello all:
    >
    > I have a list AAA = [1, 2, 3] and would like to subtract one from list
    > AAA
    > so AAA' = [0, 1, 2]


    You've had some excellent suggestions as to how to go about this
    assuming that by "efficient" you mean in terms of CPU. If, instead,
    you are concerned with memory usage, you might instead do something
    like:

    for index, value in enumerate(AAA):
    AAA[index] = value-1

    --
    Cheers,
    Simon B,
     
    Simon Brunning, Sep 19, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff User

    Best way to proceed

    Jeff User, Dec 7, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    422
    Robbe Morris [C# MVP]
    Dec 7, 2005
  2. Andy
    Replies:
    34
    Views:
    1,442
    Kelsey Bjarnason
    Jan 1, 2004
  3. Brian Schröder

    NArray scalar addition and subtraction

    Brian Schröder, Nov 28, 2004, in forum: Ruby
    Replies:
    1
    Views:
    115
    Brian Schröder
    Nov 28, 2004
  4. Zunaster
    Replies:
    1
    Views:
    106
    Harry
    Mar 1, 2007
  5. Jai
    Replies:
    99
    Views:
    670
    Joshua Landau
    Dec 26, 2013
Loading...

Share This Page