3 from collections import Counter
4 from itertools import islice
5 from typing import Any, Iterator, List, Mapping, Sequence
8 def shard(lst: List[Any], size: int) -> Iterator[Any]:
10 Yield successive size-sized shards from lst.
12 >>> for sublist in shard([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 3):
13 ... [_ for _ in sublist]
20 for x in range(0, len(lst), size):
21 yield islice(lst, x, x + size)
24 def flatten(lst: List[Any]) -> List[Any]:
28 >>> flatten([ 1, [2, 3, 4, [5], 6], 7, [8, [9]]])
29 [1, 2, 3, 4, 5, 6, 7, 8, 9]
34 if isinstance(lst[0], list):
35 return flatten(lst[0]) + flatten(lst[1:])
36 return lst[:1] + flatten(lst[1:])
39 def prepend(item: Any, lst: List[Any]) -> List[Any]:
41 Prepend an item to a list.
43 >>> prepend('foo', ['bar', 'baz'])
51 def population_counts(lst: List[Any]) -> Mapping[Any, int]:
53 Return a population count mapping for the list (i.e. the keys are
54 list items and the values are the number of occurrances of that
55 list item in the original list.
57 >>> population_counts([1, 1, 1, 2, 2, 3, 3, 3, 4])
58 Counter({1: 3, 3: 3, 2: 2, 4: 1})
64 def most_common_item(lst: List[Any]) -> Any:
67 Return the most common item in the list. In the case of ties,
68 which most common item is returned will be random.
70 >>> most_common_item([1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4])
74 return population_counts(lst).most_common(1)[0][0]
77 def least_common_item(lst: List[Any]) -> Any:
79 Return the least common item in the list. In the case of
80 ties, which least common item is returned will be random.
82 >>> least_common_item([1, 1, 1, 2, 2, 3, 3, 3, 4])
86 return population_counts(lst).most_common()[-1][0]
89 def dedup_list(lst: List[Any]) -> List[Any]:
91 Remove duplicates from the list performantly.
93 >>> dedup_list([1, 2, 1, 3, 3, 4, 2, 3, 4, 5, 1])
100 def uniq(lst: List[Any]) -> List[Any]:
102 Alias for dedup_list.
104 return dedup_list(lst)
107 def ngrams(lst: Sequence[Any], n):
109 Return the ngrams in the sequence.
111 >>> seq = 'encyclopedia'
112 >>> for _ in ngrams(seq, 3):
125 >>> seq = ['this', 'is', 'an', 'awesome', 'test']
126 >>> for _ in ngrams(seq, 3):
129 ['is', 'an', 'awesome']
130 ['an', 'awesome', 'test']
132 for i in range(len(lst) - n + 1):
136 if __name__ == '__main__':