1

I have a list of lists such as: [[foo,1],[baz,1],[foo,0],[bar,3],[foo,1],[bar,2],[baz,2]]. I want to get all the different items in the inner lists and find the total number of them. I mean the result should be like: [[foo,2],[bar,5],[baz,3]]. How can I do this task?

Thanks in advance.

1

4 Answers 4

6

Create a dictionary

D = {}
for item in list:
  left,right=item
  D[left] = D.get(left, 0) + right

There may be faster ways to do this though.

As suggested in the comments by Joce, Gnibbler and Blair you coud do this to get a list again.

# To get a list of lists
pairs = map(list, D.items()) 
# To get a list of tuples
pairs = D.items()
Sign up to request clarification or add additional context in comments.

7 Comments

You could add pairs = [(k, v) for (k, v) in D.iteritems()] for retrieval in a list.
pairs = D.items() would do the same thing.
@Blair, I think Joce made a typo. Instead of having [[k,v] ..] he may have put in [(k, v)..]
Would unpack item to (left,right) in the for statement better than this expression?
Blair is right. D.items() is better. And on this, I remove my mediocre answer! :-p
|
4

The defaultdict makes this fairly easy:

import collections
items = [['foo',1],['baz',1],['foo',0],['bar',3],['foo',1],['bar',2],['baz',2]]

totals = collections.defaultdict(int)
for key, value in items:
    totals[key] += value

print totals

When run, this gives

defaultdict(<type 'int'>, {'bar': 5, 'foo': 2, 'baz': 3})

If you want a list output, just pull the items from the dictionary

print totals.items()

and you get

[('bar', 5), ('foo', 2), ('baz', 3)]

If you really want a list-of-lists at the end,

print [list(item) for item in totals.items()]

which gives you

[['bar', 5], ['foo', 2], ['baz', 3]]

Comments

0

You can use itertools.groupby.

>>> import operator
>>> import itertools
>>> data = [['foo',1],['baz',1],['foo',0],['bar',3],['foo',1],['bar',2],['baz',2
]]
>>> {i:sum(k[1] for k in j)
...  for i, j in itertools.groupby(sorted(data, key=operator.itemgetter(0)),
...                                key=operator.itemgetter(0))}
{'baz': 3, 'foo': 2, 'bar': 5}

Comments

0

If you are using Python 3.2 (and 2.7) then you can do:

>>> from collections import Counter
>>> items = [['foo',1],['baz',1],['foo',0],['bar',3],['foo',1],['bar',2],['baz',2]]
>>> Counter(sum(( [key]*count for key,count in items), []))
Counter({'bar': 5, 'baz': 3, 'foo': 2})
>>> Counter(sum(( [key]*count for key,count in items), [])).most_common()
[('bar', 5), ('baz', 3), ('foo', 2)]
>>> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.