How do you find out where all items x are in a n-dimensional list of any shape or size? (Example: [1, 2, [3, [1]]...])
This is the code that I came up with for finding the first item: (Not heavily tested)
def where(x, inputList):
return _where(x, inputList, [])
def _where(value, inputList, dimension):
if isinstance(inputList, list):
i = 0
for l in inputList:
dimension.append(i)
D = _where(value, l, dimension)
if not D == None:
return D
i += 1
del dimension[-1]
return None
else:
if value == inputList:
return dimension
else:
return None
It recursively checks every item in the list and when it finds the correct one it returns the dimension or the coordinates of that item
Desired Input/Output Example:
x = 1
inputlist = [1, [2, 21], [1]]
o = where_All(x, inputlist)
# o is [[0], [2, 0]]
print inputlist[0] # is 1
print inputlist[2][0] # is 1
O is a list of coordinates for each item in the list which equals x