I\'ve been working with some relatively complex arrays such as:
array = [ \"1\", 2, [\"4\", \"5\", (\"a\", \"b\")], (\"c\", \"d\")]
and I was l
array = [ "1", 2, ["4", "5", ("a", "b")], ("c", "d")]
def find_index(array, item, index=None):
if not index:
index = []
try:
i = array.index(item)
except:
for new_array in array:
if hasattr(new_array, '__iter__'):
i = find_index(new_array, item, index+[array.index(new_array)])
if i:
return i
else:
return index + [i]
return None
This gives:
>>> find_index(array, 1)
>>> find_index(array, "1")
[0]
>>> find_index(array, 2)
[1]
>>> find_index(array, "4")
[2, 0]