X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=list_utils.py;h=533317eb6da71f44a4f8b3c338505c053c460c0b;hb=c41e0e59446412511c5737cf5b6ba8f289e75e7e;hp=993ca8af9ca9f9a633c02162e0f155fd3b3cab74;hpb=1fa049c0ce2af2027e362e368321636c75c987de;p=python_utils.git diff --git a/list_utils.py b/list_utils.py index 993ca8a..533317e 100644 --- a/list_utils.py +++ b/list_utils.py @@ -1,7 +1,8 @@ #!/usr/bin/env python3 +from collections import Counter from itertools import islice -from typing import Any, Iterator, List +from typing import Any, Iterator, List, Mapping def shard(lst: List[Any], size: int) -> Iterator[Any]: @@ -47,6 +48,44 @@ def prepend(item: Any, lst: List[Any]) -> List[Any]: return lst +def population_counts(lst: List[Any]) -> Mapping[Any, int]: + """ + Return a population count mapping for the list (i.e. the keys are + list items and the values are the number of occurrances of that + list item in the original list. + + >>> population_counts([1, 1, 1, 2, 2, 3, 3, 3, 4]) + Counter({1: 3, 3: 3, 2: 2, 4: 1}) + + """ + return Counter(lst) + + +def most_common_item(lst: List[Any]) -> Any: + + """ + Return the most common item in the list. In the case of ties, + which most common item is returned will be random. + + >>> most_common_item([1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4]) + 3 + + """ + return population_counts(lst).most_common(1)[0][0] + + +def least_common_item(lst: List[Any]) -> Any: + """ + Return the least common item in the list. In the case of + ties, which least common item is returned will be random. + + >>> least_common_item([1, 1, 1, 2, 2, 3, 3, 3, 4]) + 4 + + """ + return population_counts(lst).most_common()[-1][0] + + if __name__ == '__main__': import doctest doctest.testmod()