syntax question

Discussion in 'Python' started by AF, Apr 5, 2004.

  1. AF

    AF Guest

    If I have a list of touples:

    l = [(x1, y1), (x2, y2), ...]

    Is there a 1 line way to extract and get the sum of each x and y
    column. I can do it this way with 2 lines of code and iterating
    through the list twice:

    sumx = sum([x for x, y in l])
    sumy = sum([y for x, y in l])

    Is there a 1 liner way to get the sums of both x and y and only
    iterate thru the list once?

    Also, is there a way to extract a list of x's and a list of y's from
    the touple list? 1 line of course.

    Thanks!

    AF
    AF, Apr 5, 2004
    #1
    1. Advertising

  2. AF wrote:

    > If I have a list of touples:
    >
    > l = [(x1, y1), (x2, y2), ...]
    >
    > Is there a 1 line way to extract and get the sum of each x and y
    > column. I can do it this way with 2 lines of code and iterating
    > through the list twice:
    >
    > sumx = sum([x for x, y in l])
    > sumy = sum([y for x, y in l])
    >
    > Is there a 1 liner way to get the sums of both x and y and only
    > iterate thru the list once?
    >
    > Also, is there a way to extract a list of x's and a list of y's from
    > the touple list? 1 line of course.


    Use zip:

    >>> l = ((1, 2), (3, 4), (5, 6))
    >>> zip(*l)

    [(1, 3, 5), (2, 4, 6)]
    >>> xSum, ySum = map(sum, zip(*l))
    >>> xSum

    9
    >>> ySum

    12

    --
    __ Erik Max Francis && && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    \__/ I do this for the love of music / Not for the glitter and gold
    -- India Arie
    Erik Max Francis, Apr 5, 2004
    #2
    1. Advertising

  3. >>> L=[(1,2),(10,20),(100,200)]
    >>> map(None,*L)

    [(1, 10, 100), (2, 20, 200)]
    >>> sum(map(None,*L)[0])

    111

    "AF" <> wrote in message
    news:...
    | If I have a list of touples:
    |
    | l = [(x1, y1), (x2, y2), ...]
    |
    | Is there a 1 line way to extract and get the sum of each x and y
    | column. I can do it this way with 2 lines of code and iterating
    | through the list twice:
    |
    | sumx = sum([x for x, y in l])
    | sumy = sum([y for x, y in l])
    |
    | Is there a 1 liner way to get the sums of both x and y and only
    | iterate thru the list once?
    |
    | Also, is there a way to extract a list of x's and a list of y's from
    | the touple list? 1 line of course.
    |
    | Thanks!
    |
    | AF
    Elaine Jackson, Apr 6, 2004
    #3
    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. gabriele renzi
    Replies:
    2
    Views:
    189
    gabriele renzi
    Dec 31, 2005
  2. Ken Bloom
    Replies:
    3
    Views:
    182
  3. Good Night Moon
    Replies:
    9
    Views:
    258
    Rick DeNatale
    Jul 25, 2007
  4. Jacob Grover
    Replies:
    5
    Views:
    299
    Jacob Grover
    Jul 18, 2008
  5. Mark Richards
    Replies:
    3
    Views:
    297
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page