summaryrefslogtreecommitdiff
path: root/list_utils.py
blob: a8030e30812ceb804a6c45ff386a7ab159fa2911 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3

from collections import Counter
from itertools import islice
from typing import Any, Iterator, List, Mapping, Sequence


def shard(lst: List[Any], size: int) -> Iterator[Any]:
    """
    Yield successive size-sized shards from lst.

    >>> for sublist in shard([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 3):
    ...     [_ for _ in sublist]
    [1, 2, 3]
    [4, 5, 6]
    [7, 8, 9]
    [10, 11, 12]

    """
    for x in range(0, len(lst), size):
        yield islice(lst, x, x + size)


def flatten(lst: List[Any]) -> List[Any]:
    """
    Flatten out a list:

    >>> flatten([ 1, [2, 3, 4, [5], 6], 7, [8, [9]]])
    [1, 2, 3, 4, 5, 6, 7, 8, 9]

    """
    if len(lst) == 0:
        return lst
    if isinstance(lst[0], list):
        return flatten(lst[0]) + flatten(lst[1:])
    return lst[:1] + flatten(lst[1:])


def prepend(item: Any, lst: List[Any]) -> List[Any]:
    """
    Prepend an item to a list.

    >>> prepend('foo', ['bar', 'baz'])
    ['foo', 'bar', 'baz']

    """
    lst.insert(0, item)
    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]


def dedup_list(lst: List[Any]) -> List[Any]:
    """
    Remove duplicates from the list performantly.

    >>> dedup_list([1, 2, 1, 3, 3, 4, 2, 3, 4, 5, 1])
    [1, 2, 3, 4, 5]

    """
    return list(set(lst))


def uniq(lst: List[Any]) -> List[Any]:
    """
    Alias for dedup_list.
    """
    return dedup_list(lst)


def ngrams(lst: Sequence[Any], n):
    """
    Return the ngrams in the sequence.

    >>> seq = 'encyclopedia'
    >>> for _ in ngrams(seq, 3):
    ...     _
    'enc'
    'ncy'
    'cyc'
    'ycl'
    'clo'
    'lop'
    'ope'
    'ped'
    'edi'
    'dia'

    >>> seq = ['this', 'is', 'an', 'awesome', 'test']
    >>> for _ in ngrams(seq, 3):
    ...     _
    ['this', 'is', 'an']
    ['is', 'an', 'awesome']
    ['an', 'awesome', 'test']
    """
    for i in range(len(lst) - n + 1):
        yield lst[i:i + n]


if __name__ == '__main__':
    import doctest
    doctest.testmod()