I need help checking if all the items in a two-dimensional list are the same (in this case, I’m checking if they are all equal to one).
I made a function allOnes(L) that checks if all items are 1’s in a 1D array. I used the all() function like this:
def allOnes(L):
"""Tests to see if the numbers in the list L are all 1's
"""
return all(x == 1 for x in L)
Now I need to check if all items in a 2D list are all 1’s. I would like the function allOnes2d to return True when it checks a list like this: [[1,1,1], [1,1,1], [1,1,1]]. Is this possible using all()?
And to do an array of any dimension, use itertools.chain to reduce the N-dimensional array to a regular one, then give it to your allOnes function. (Thanks to Lattyware)