How to efficiently proceed addition and subtraction in python list?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Mark

    How to efficiently proceed addition and subtraction in python list?

    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

  • Tim Chase

    #2
    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



    Comment

    • Chiucs

      #3
      Re: How to efficiently proceed addition and subtraction in python list?

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

      "Daniel Mark" <danielmarkhot@ gmail.com???
      news:1158619411 .713865.287810@ m73g2000cwd.goo glegroups.com ???...
      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
      >

      Comment

      • Paul Rubin

        #4
        Re: How to efficiently proceed addition and subtraction in python list?

        "Daniel Mark" <danielmarkhot@ gmail.comwrites :
        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]

        Comment

        • Ant

          #5
          Re: How to efficiently proceed addition and subtraction in python list?


          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.

          Comment

          • Steve Holden

            #6
            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.4686168214116 599
            >>t = timeit.Timer("b = [x-1 for x in a]", setup="a=[1,2,3]")
            >>t.timeit()
            0.9930245324475 635
            >>>
            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

            Comment

            • Fredrik Lundh

              #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.4686168214116 599
              >>t = timeit.Timer("b = [x-1 for x in a]", setup="a=[1,2,3]")
              >>t.timeit()
              0.9930245324475 635
              >>>
              And now someone's probably reading this and thinking that list comprehensions
              are *always* faster than "map"...

              </F>



              Comment

              • Steve Holden

                #8
                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.46861682141 16599
                >>t = timeit.Timer("b = [x-1 for x in a]", setup="a=[1,2,3]")
                >>t.timeit()
                >>0.99302453244 75635
                >>>
                >
                >
                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

                Comment

                • Ant

                  #9
                  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.

                  Comment

                  • Simon Brunning

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

                    On 18 Sep 2006 15:43:31 -0700, Daniel Mark <danielmarkhot@ gmail.comwrote:
                    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@brunningo nline.net

                    Comment

                    Working...