-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounterExample.py
More file actions
32 lines (24 loc) · 872 Bytes
/
counterExample.py
File metadata and controls
32 lines (24 loc) · 872 Bytes
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
#counter extend class sample
from collections import Counter
c = Counter()
c['apples'] += 1
c['bananas'] += 1
c['apples'] += 2
c['cherries'] += 4
print c.most_common() #[('cherries', 4), ('apples', 3), ('bananas', 1)]
print c['lemons']
prose = "Python is so easy to learn and use, that it also might be easy to re-invent the wheel for common tasks. Resist the temptation as the Python Standard Library might have you covered!"
prose = prose.lower()
words = prose.split(' ')
prose_count = Counter()
for word in words:
prose_count[word[0]] += 1
print prose_count.most_common()
print prose_count.elements()
print prose_count.keys()
fruits = ['apples', 'apples', 'bananas', 'cherries', 'lemons', 'oranges', 'oranges']
shipment = Counter(fruits)
c.update(shipment)
sold = ['apples', 'apples', 'oranges', 'oranges', 'bananas']
c.subtract(sold)
print c.most_common()