MCPcopy Create free account
hub / github.com/ActiveState/code / Counter

Class Counter

recipes/Python/52231_Count_items_sort/recipe-52231.py:1–15  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Counter:
2 def __init__(self):
3 self.dict = {}
4 def add(self,item):
5 count = self.dict.setdefault(item,0)
6 self.dict[item] = count + 1
7 def counts(self,desc=None):
8 '''Returns list of keys, sorted by values.
9 Feed a 1 if you want a descending sort.'''
10 i = map(lambda t: list(t),self.dict.items())
11 map(lambda r: r.reverse(),i)
12 i.sort()
13 if desc:
14 i.reverse()
15 return i
16
17if __name__ == '__main__':
18

Callers 1

recipe-52231.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected