Parallel arithmetic?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terrance N. Phillip

    #1

    Parallel arithmetic?

    Given a and b, two equal length lists of integers, I want c to be
    [a1-b1, a2-b2, ... , an-bn]. I can do something like:

    c = [0] * len(a)
    for ndx, item in enumerate(a):
    c[ndx] = item - b[ndx]

    But I'm wondering if there's a better way, perhaps that avoids a loop?

    Nick.

    (I seem to recall from my distant past that this sort of thing was dead
    easy with APL... c = a-b, more or less.)

    N
  • Paul Rubin

    #2
    Re: Parallel arithmetic?

    "Terrance N. Phillip" <mediocre_perso n@hotmail.com> writes:[color=blue]
    > Given a and b, two equal length lists of integers, I want c to be
    > [a1-b1, a2-b2, ... , an-bn].[/color]

    c = [a[i] - b[i] for i in xrange(len(a))]

    Comment

    • Michael Hoffman

      #3
      Re: Parallel arithmetic?

      Terrance N. Phillip wrote:[color=blue]
      > Given a and b, two equal length lists of integers, I want c to be
      > [a1-b1, a2-b2, ... , an-bn]. I can do something like:
      >
      > c = [0] * len(a)
      > for ndx, item in enumerate(a):
      > c[ndx] = item - b[ndx]
      >
      > But I'm wondering if there's a better way, perhaps that avoids a loop?[/color]

      Here's one way:

      c = [a_item - b_item for a_item, b_item in zip(a, b)]

      And another:

      import operator
      c = map(operator.su b, a, b)
      --
      Michael Hoffman

      Comment

      • Lonnie Princehouse

        #4
        Re: Parallel arithmetic?

        There are many ways to do this. None of them avoids looping,
        technically, although you can easily avoid the "for" syntax.

        -- Simple but wastes some memory
        c = [i-j for i,j in zip(a,b)]

        -- Using itertools.izip (python 2.3)
        c = [i-j for i,j in itertools.izip( a,b) ]

        -- Generator expression (python 2.4)
        c = ( i-j for i,j in itertools.izip( a,b) )

        Comment

        • Robert Kern

          #5
          Re: Parallel arithmetic?

          Terrance N. Phillip wrote:[color=blue]
          > Given a and b, two equal length lists of integers, I want c to be
          > [a1-b1, a2-b2, ... , an-bn]. I can do something like:
          >
          > c = [0] * len(a)
          > for ndx, item in enumerate(a):
          > c[ndx] = item - b[ndx]
          >
          > But I'm wondering if there's a better way, perhaps that avoids a loop?
          >
          > Nick.
          >
          > (I seem to recall from my distant past that this sort of thing was dead
          > easy with APL... c = a-b, more or less.)[/color]

          If you're doing this kind of thing often, look into using Numeric.



          In [21]: from Numeric import array

          In [22]: a = array(range(10) )

          In [23]: b = array(range(10, 20))

          In [24]: c = a - b

          In [25]: c
          Out[25]: [-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,]

          --
          Robert Kern
          rkern@ucsd.edu

          "In the fields of hell where the grass grows high
          Are the graves of dreams allowed to die."
          -- Richard Harter

          Comment

          • jepler@unpythonic.net

            #6
            Re: Parallel arithmetic?

            If you use numarray, you *can* write
            c = a-b
            [color=blue][color=green][color=darkred]
            >>> import numarray
            >>> a = numarray.array([1,2,3])
            >>> b = numarray.array([5,0,2])
            >>> c = a-b
            >>> c[/color][/color][/color]
            array([-4, 2, 1])

            numarray is packaged separately from Python.
            The Space Telescope Science Institute helps humanity explore the universe with advanced space telescopes and ever-growing data archives.


            Jeff

            -----BEGIN PGP SIGNATURE-----
            Version: GnuPG v1.2.6 (GNU/Linux)

            iD8DBQFC8qGZJd0 1MZaTXX0RAvAsAJ 9WfsYQZOwDnLeHu a0Wrw7kWjT/+QCgiYwT
            HfGp0vKbezN0qwO zCNF7e5A=
            =wFXg
            -----END PGP SIGNATURE-----

            Comment

            • Terry Reedy

              #7
              Re: Parallel arithmetic?

              [color=blue]
              >"#"map" will be removed from the next versions of python.[/color]

              The next version will be 2.5. Map will not go away then.
              In 3.0, in the indefinite future, it might go away, it might just be moved.

              tjr



              Comment

              • Terrance N. Phillip

                #8
                Re: Parallel arithmetic?

                Thank-you very much for all the excellent replies. I'm thinking of using
                this to determine if a sequence is a "run" (as in a card game). If I've
                got a sorted hand [3, 4, 5, 6, 7], then I know I've got a 5-card run
                because [4, 5, 6, 7] - [3, 4, 5, 6] == [1, 1, 1, 1]. I want to avoid
                something like
                if h[0] == h[1]-1 and h[1] == h[2]-1 ...

                Nick.

                Comment

                • Robert Kern

                  #9
                  Re: Parallel arithmetic?

                  Terrance N. Phillip wrote:[color=blue]
                  > Thank-you very much for all the excellent replies. I'm thinking of using
                  > this to determine if a sequence is a "run" (as in a card game). If I've
                  > got a sorted hand [3, 4, 5, 6, 7], then I know I've got a 5-card run
                  > because [4, 5, 6, 7] - [3, 4, 5, 6] == [1, 1, 1, 1]. I want to avoid
                  > something like
                  > if h[0] == h[1]-1 and h[1] == h[2]-1 ...[/color]

                  In that case:



                  --
                  Robert Kern
                  rkern@ucsd.edu

                  "In the fields of hell where the grass grows high
                  Are the graves of dreams allowed to die."
                  -- Richard Harter

                  Comment

                  • Dennis Lee Bieber

                    #10
                    Re: Parallel arithmetic?

                    On Thu, 04 Aug 2005 20:59:33 -0500, "Terrance N. Phillip"
                    <mediocre_perso n@hotmail.com> declaimed the following in
                    comp.lang.pytho n:
                    [color=blue]
                    > Thank-you very much for all the excellent replies. I'm thinking of using
                    > this to determine if a sequence is a "run" (as in a card game). If I've
                    > got a sorted hand [3, 4, 5, 6, 7], then I know I've got a 5-card run[/color]

                    A sorted list?

                    if (hand[-1] - hand[0]) == (len(hand) - 1)

                    would seem to do it for your example.

                    Actually, if you KNOW the list is only 5 entries long

                    if (hand[4] - hand[0]) == 4

                    would do it.

                    Or any equivalent... (hand[0] + 4) == hand[4]


                    --[color=blue]
                    > =============== =============== =============== =============== == <
                    > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                    > wulfraed@dm.net | Bestiaria Support Staff <
                    > =============== =============== =============== =============== == <
                    > Home Page: <http://www.dm.net/~wulfraed/> <
                    > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

                    Comment

                    • jburgy

                      #11
                      Re: Parallel arithmetic?

                      Dennis Lee Bieber wrote:[color=blue]
                      > On Thu, 04 Aug 2005 20:59:33 -0500, "Terrance N. Phillip"
                      > <mediocre_perso n@hotmail.com> declaimed the following in
                      > comp.lang.pytho n:
                      >[color=green]
                      > > Thank-you very much for all the excellent replies. I'm thinking of using
                      > > this to determine if a sequence is a "run" (as in a card game). If I've
                      > > got a sorted hand [3, 4, 5, 6, 7], then I know I've got a 5-card run[/color]
                      >
                      > A sorted list?
                      >
                      > if (hand[-1] - hand[0]) == (len(hand) - 1)
                      >
                      > would seem to do it for your example.
                      >
                      > Actually, if you KNOW the list is only 5 entries long
                      >
                      > if (hand[4] - hand[0]) == 4
                      >[/color]

                      It's cute but wrong! How 'bout hand = [ 0, 0, 0, 4 ]? It's sorted,
                      passes your test and does not meet the OP's requirement :(
                      [color=blue]
                      > would do it.
                      >
                      > Or any equivalent... (hand[0] + 4) == hand[4]
                      >
                      >
                      > --[color=green]
                      > > =============== =============== =============== =============== == <
                      > > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                      > > wulfraed@dm.net | Bestiaria Support Staff <
                      > > =============== =============== =============== =============== == <
                      > > Home Page: <http://www.dm.net/~wulfraed/> <
                      > > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color][/color]

                      Comment

                      Working...