1from collections import OrderedDict
2
3# Remembers the order the keys are added!
4x = OrderedDict(a=1, b=2, c=3)
1LIST: Can store duplicate values,Keeps the insertion order.
2- ArrayList not syncronized, array based class
3- LinkedList not synchronized, doubly linked
4- Vector is synchronized, thread safe
5SET: Can only store unique values,And does not maintain order
6- HashSet can have null, order is not guaranteed
7- LinkedHashSet can have null and keeps the order
8- TreeSet sorts the order and don't accept null
9QUQUE :Accepts duplicates, Doesn't have index num,
10First in first our order.
11MAP:is a (key-value format)
12and keys are always unique,
13and value can be duplicated.
14- HashTable don't have null key, sychronized(thread-safe)
15- LinkedHashMap can have null key, keeps order
16- HasHMap can have null key, order is not guaranteed
17- TreeMap doesn't have null key and keys are sorted
18Examples
19 ■ I have used TreeSet to print dropdown list in for
20 non duplicate values and ascending order
21■ I have used HashMaps to compare values from
22a database with expected values
1>>> # regular unsorted dictionary
2>>> d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}
3
4>>> # dictionary sorted by key
5>>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))
6OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])
7
8>>> # dictionary sorted by value
9>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))
10OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])
11
12>>> # dictionary sorted by length of the key string
13>>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
14OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])
15
1most_common([n])¶
2Return a list of the n most common elements and their counts from the most common to the least. If n is omitted or None, most_common() returns all elements in the counter.
3Elements with equal counts are ordered arbitrarily:
4
5>>> Counter('abracadabra').most_common(3)
6[('a', 5), ('r', 2), ('b', 2)]
7
1import collections
2
3numShoes = int(raw_input())
4shoes = collections.Counter(map(int, raw_input().split()))
5numCust = int(raw_input())
6
7income = 0
8
9for i in range(numCust):
10 size, price = map(int, raw_input().split())
11 if shoes[size]:
12 income += price
13 shoes[size] -= 1
14
15print income
16