syntax question

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

    syntax question

    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
  • Erik Max Francis

    #2
    Re: syntax question

    AF wrote:
    [color=blue]
    > 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.[/color]

    Use zip:
    [color=blue][color=green][color=darkred]
    >>> l = ((1, 2), (3, 4), (5, 6))
    >>> zip(*l)[/color][/color][/color]
    [(1, 3, 5), (2, 4, 6)][color=blue][color=green][color=darkred]
    >>> xSum, ySum = map(sum, zip(*l))
    >>> xSum[/color][/color][/color]
    9[color=blue][color=green][color=darkred]
    >>> ySum[/color][/color][/color]
    12

    --
    __ Erik Max Francis && max@alcyone.com && 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

    Comment

    • Elaine Jackson

      #3
      Re: syntax question

      >>> L=[(1,2),(10,20),( 100,200)][color=blue][color=green][color=darkred]
      >>> map(None,*L)[/color][/color][/color]
      [(1, 10, 100), (2, 20, 200)][color=blue][color=green][color=darkred]
      >>> sum(map(None,*L )[0])[/color][/color][/color]
      111

      "AF" <AnthonyF@Blarg .NET> wrote in message
      news:c10dc8.040 4051323.4594717 c@posting.googl e.com...
      | 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


      Comment

      Working...