Make sure all the elements in the list are the same in Python

I was reluctant to keep disclosing incorrect information, so I rewrote it. I tried to write several ways while reflecting the points I received.

Implementation

Judge by hitting the first element with another element

def map_all(es):
    return all([e == es[0] for e in es[1:]]) if es else False

Judge by the number of elements when duplicate elements are deleted

def set_list(es):
    return len(list(set(es))) == 1

Judgment by hitting the first element with another element (recursive)

def recursive(es):
    return _rec(es, es[0], True) if es else False

def _rec(es, head, acc):
    return _rec(es[1:], head, acc and head == es[0]) if es else acc

Judgment by duplicating the first element and comparing

def multi_diff(es):
    return es == [es[0]] * len(es) if es else False

test

Last time it was so good that I tested it to some extent this time ...

def tests(expected, elements):
    assert map_all(elements) == expected
    assert set_list(elements) == expected
    assert recursive(elements) == expected
    assert multi_diff(elements) == expected

T, F, N = True, False, None

tests(True, [-1])
tests(True, [0])
tests(True, [1])
tests(True, [2])
tests(True, ['T'])
tests(True, [T])
tests(True, [N])

tests(True, [-1, -1])
tests(True, [0, 0])
tests(True, [1, 1])
tests(True, [2, 2])
tests(True, ['T', 'T'])
tests(True, [T, T])
tests(True, [F, F])
tests(True, [N, N])

tests(True, [-1, -1, -1])
tests(True, [0, 0, 0])
tests(True, [1, 1, 1])
tests(True, [2, 2, 2])
tests(True, ['T', 'T', 'T'])
tests(True, [T, T, T])
tests(True, [F, F, F])
tests(True, [N, N, N])


tests(False, [])

tests(False, [0, 1])
tests(False, [1, 0])
tests(False, [0, 2])
tests(False, [1, 2])
tests(False, ['T', 'F'])
tests(False, ['F', 'T'])
tests(False, [T, F])
tests(False, [F, T])
tests(False, [T, N])
tests(False, [N, F])
tests(False, [N, 0])
tests(False, [0, N])

tests(False, [0, 0, 1])
tests(False, [0, 1, 0])
tests(False, [T, F, F])
tests(False, [F, T, F])
tests(False, [F, F, T])
tests(False, [F, T, T])
tests(False, [T, F, T])
tests(False, [T, T, F])
tests(False, [T, T, N])


tests(True, [[]])
tests(True, [[], []])
tests(True, [[], [], []])
tests(True, [[5]])
tests(True, [[5], [5]])
tests(True, [[5], [5], [5]])

tests(False, [[],  [5]])
tests(False, [[3], [5]])
tests(False, [[3], [3, 3]])

in conclusion

Last time I said this, oh embarrassing ...

Recommended Posts

Make sure all the elements in the list are the same in Python
[Python] Outputs all combinations of elements in the list
[Python] Combine all the elements in the array
Determine if all list elements are present in the dict key
[python] Check the elements of the list all, any
Make a copy of the list in Python
Getting list elements in Python
Sort and output the elements in the list as elements and multiples in Python.
Get the number of specific elements in a python list
Delete multiple elements in python list
[PyTorch] Make sure the model and dataset are in cuda mode
python Note: Determine if command line arguments are in the list
All elements move (do not remain in the same position) shuffle
OR the List in Python (zip function)
[python] Permutation generation considering the same elements
Get the EDINET code list in Python
Python open and io.open are the same
[Python] Sort the list of pathlib.Path in natural sort
Check if the characters are similar in Python
Get only the subclass elements in a list
Randomly select elements from list (array) in python
Sort list elements in a specified order in Python
[Python] Manipulating elements in a list (array) [Sort]
How to remove duplicate elements in Python3 list
[Python] Solution to the problem that elements are linked when copying a list
How to get a list of files in the same directory with python
Sorted list in Python
Filter List in Python
List find in Python
About __all__ in python
[python] Get the list of classes defined in the module
[Python] Manipulation of elements in list (array) [Add / Delete]
Get the size (number of elements) of UnionFind in Python
Group by consecutive elements of a list in Python
[Python] How to output the list values in order
Using the LibreOffice app in Python (1) Where are the macros?
[Python] Make sure the received function is a user-defined function
How to check in Python if one of the elements of a list is in another list
Download the file in Python
Create a list in Python with all followers on twitter
Find the difference in Python
Don't make test.py in Python!
How to check if the contents of the dictionary are the same in Python by hash value
Make a bookmarklet in Python
How to get the last (last) value in a list in Python
Methods available in the list
Make Opencv available in Python
Make python segfault in 2 lines
Create a new list by combining duplicate elements in the list
I want to make the Dictionary type in the List unique
[Python3] "A // B" and "math.floor (A / B)" are not always the same! ??
To make sure that the specified key is in the specified bucket in Boto 3
Python> list> append () and extend ()> append: list is added | extend: list elements are added | + = to add list
[Python] Display only the elements of the list side by side [Vertical, horizontal]
[Python] Let's reduce the number of elements in the result in set operations
Make the morphological analysis engine MeCab available in Python 3 (March 2016 version)
python: Use the variables defined in the string format as they are
How to use the asterisk (*) in Python. Maybe this is all? ..
[python] Get the rank of the values in List in ascending / descending order
Throw something to Kinesis with python and make sure it's in
The story that 2D list replacement did not work in python