collections module

collections_utilities

py_utilities.collections.collections_utilities.flatten(lst)[source]

Flattens a list

Taken from: https://stackoverflow.com/questions/2158395/ flatten-an-irregular-list-of-lists-in-python/17868434#17868434

>>> list(flatten([1,2,[1],[1,2,3]]))
[1, 2, 1, 1, 2, 3]
>>> list(flatten([]))
[]
>>> list(flatten([1,2,3]))
[1, 2, 3]
py_utilities.collections.collections_utilities.has_dupes(iterable)[source]

Returns true if iterable contains dupes. All values in the iterable need to be hashable

>>> has_dupes(['1', '2', '3', '3'])
True
>>> has_dupes(['1', '2', '3', '4'])
False
>>> has_dupes(('1', '1', '3', '4'))
True
>>> has_dupes((1, 2, 3, 4))
False
>>> has_dupes([])
False
py_utilities.collections.collections_utilities.index_max(seq)[source]

Returns the index of the first occurrence maxima in a sequence

>>> index_max([1,2,3,4,5,6,1,2,1])
5
>>> index_max([9,2,3,4,5,6,1,2,1])
0
>>> index_max([])
Traceback (most recent call last):
    ...
ValueError: max() arg is an empty sequence
py_utilities.collections.collections_utilities.index_min(seq)[source]

Returns the index of the first occurrence minima in a sequence

>>> index_min([1,2,3,4,5,6,1,2,1])
0
>>> index_min([2,2,3,4,5,6,1,2,1])
6
>>> index_min([])
Traceback (most recent call last):
    ...
ValueError: min() arg is an empty sequence
py_utilities.collections.collections_utilities.unique(lst)[source]

Returns the unique values in a list lst

>>> unique([1,1,1,1,1,1,1])
[1]
>>> x = unique([1,1,1,1,1,1,1,2])
>>> 1 in x
True
>>> 2 in x
True
>>> len(x)
2
>>> x = unique([-1,1,1,1,1,1,1,2])
>>> 1 in x
True
>>> -1 in x
True
>>> 2 in x
True
>>> len(x)
3
>>> x = unique([-1,2,'1'])
>>> 1 in x
False
>>> -1 in x
True
>>> 2 in x
True
>>> '1' in x
True
>>> len(x)
3
py_utilities.collections.collections_utilities.value_max(seq)[source]

Returns the value of the first occurrence maxima in a sequence

>>> value_max([1,2,3,4,5,6,1,2,1])
6
>>> value_max([2,2,3,4,5,6,8,2,9])
9
>>> value_max([])
Traceback (most recent call last):
    ...
ValueError: max() arg is an empty sequence
py_utilities.collections.collections_utilities.value_min(seq)[source]

Returns the value of the first occurrence minima in a sequence

>>> value_min([1,2,3,4,5,6,1,2,1])
1
>>> value_min([2,2,3,4,5,6,8,2,9])
2
>>> value_min([])
Traceback (most recent call last):
    ...
ValueError: min() arg is an empty sequence