Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > syntax question

Reply
Thread Tools

syntax question

 
 
AF
Guest
Posts: n/a
 
      04-05-2004
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
 
Reply With Quote
 
 
 
 
Erik Max Francis
Guest
Posts: n/a
 
      04-05-2004
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
 
Reply With Quote
 
 
 
 
Elaine Jackson
Guest
Posts: n/a
 
      04-06-2004
>>> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax Checker that's better than the normal syntax checker Jacob Grover Ruby 5 07-18-2008 05:07 AM
Syntax error? What syntax error? Assignment fo default values? Mark Richards Perl Misc 3 11-18-2007 05:01 PM
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
[ANN] SqlStatement 1.0.0 - hide the syntax of SQL behind familiarruby syntax Ken Bloom Ruby 3 10-09-2006 06:46 PM
Syntax highligth with textile: Syntax+RedCloth ? gabriele renzi Ruby 2 12-31-2005 02:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57