from collections import Counter
lst = [1, 2, 3, 3, 2, 1, 1, 1, 2, 2, 3, 1, 2, 1, 1]counter = Counter(lst)
Counter({1: 7, 2: 5, 3: 3})
lst = [1, 2, 3, 3, 2, 1, 1, 1, 2, 2, 3, 1, 2, 1, 1]counter = Counter(lst)print(counter[1])
lst = [1, 2, 3, 3, 2, 1, 1, 1, 2, 2, 3, 1, 2, 1, 1]counter = Counter(lst)print(counter.most_common(2))
[(1,7),(2,5)]
from collections import defaultdictnames_dict = defaultdict(int)names_dict["Bob"] = 1names_dict["Katie"] = 2sara_number = names_dict["Sara"]print(names_dict)
defaultdict(,{'bob':1,'katie':2,'sara':0})
defaultdict(,{'bob':1,'katie':2,'sara':[]})
from collections import dequemy_queue = deque(maxlen=10)for i in range(10): my_queue.append(i+1)print(my_queue)
deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], maxlen=10)
for i in range(10, 15): my_queue.append(i+1)print(my_queue)
deque([6, 7, 8, 9, 10, 11, 12, 13, 14, 15], maxlen=10)
from collections import namedtuplePerson = namedtuple('Person', 'name age job')
Person = namedtuple('Person', 'name age job')Mike = Person(name='Mike', age=30, job='Data Scientist')Kate = Person(name="Kate", age=28, job='Project Manager')print(Mike)print(Kate)
Person(name='Mike', age=30, job='Data Scientist')Person(name='Kate', age=28, job='Project Manager')