Can I do a reduce on a list comprehension into two lists, based on two values?

Posted by pdknsk on Stack Overflow See other posts from Stack Overflow or by pdknsk
Published on 2010-12-27T15:28:38Z Indexed on 2010/12/27 15:54 UTC
Read the original article Hit count: 169

Filed under:
|

I've got the following code.

sum_review = reduce(add,[book['rw'] for book in books])
sum_rating = reduce(add,[book['rg'] for book in books])
items = len(books)
avg_review = sum_review/items
avg_rating = sum_rating/items

What I'd like is this.

sum_review,sum_rating = reduce(add,([book['rw'],[book['rg']) for book in books])
items = len(books)
avg_review = sum_review/items
avg_rating = sum_rating/items

Obviously this doesn't work. How can I solve this redundancy, without a regular loop?

© Stack Overflow or respective owner

Related posts about python

Related posts about python-2.5