For a one dimensional list, the index of an item is found as follows:
a_list = [\'a\', \'b\', \'new\', \'mpilgrim\', \'new\']
a_list.index(\'mpilgrim\')
For n-dimensional recursive search, you can try something like this:
from copy import copy
def scope(word, list, indexes = None):
result = []
if not indexes:
indexes = []
for index, item in enumerate(list):
try:
current_index = indexes + [index]
result.append(current_index + [item.index(word)])
except ValueError:
pass
if type(item[0]) == type([]):
indexes.append(index)
result.extend(scope(word, item, copy(indexes)))
return result
And the result is:
>>> d_list = [['a', 'b', 'new', 'mpilgrim', 'new'], [['a', 'b', 'new', 'mpilgrim', 'new'], ['b', 'd', 'new', 'mpilgrim', 'new']]]
>>> word = 'mpilgrim'
>>> result = scope(word, d_list)
[[0, 3], [1, 0, 3], [1, 1, 3]]
Probably there are better ways to do it, but that is the one I figured out without getting any library.
EDIT: Actually, it was not perfect and one library must be added. It's copy. Now it's ok.
For two dimensional list; you can iterate over rows and using .index function for looking for item:
def find(l, elem):
for row, i in enumerate(l):
try:
column = i.index(elem)
except ValueError:
continue
return row, column
return -1
tl = [[1,2,3],[4,5,6],[7,8,9]]
print(find(tl, 6)) # (1,2)
print(find(tl, 1)) # (0,0)
print(find(tl, 9)) # (2,2)
print(find(tl, 12)) # -1
For multidimensional arrays:
def find(needle,haystack):
if needle == haystack: return []
# Strings are iterable, too
if isinstance(haystack,str) and len(haystack)<=1: return None
try:
for i,e in enumerate(haystack):
r = find(needle,e)
if r is not None:
r.insert(0,i)
return r
except TypeError:
pass
return None
ml = [[1,2,3],[4,5,6],[7,8,9]]
print find(2,ml)
ml = [3,[[1,2,3],[4,5,6],[7,8,9]]]
print find(2,ml)
ml = [[["ab", "bc", "cde"]]]
print find("d",ml)
There should be a better way to avoid the try/except block, but I could not find one: In Python, how do I determine if an object is iterable?
I don't know of an automatic way to do it, but if
a = [[1,2],[3,4],[5,6]]
and you want to find the location of 3, you can do:
x = [x for x in a if 3 in x][0]
print 'The index is (%d,%d)'%(a.index(x),x.index(3))
The output is:
The index is (1,0)
list_2d = [[1,2],[3,4],[5,6]]
element = 1
index_row = [list_2d.index(row) for row in list_2d if element in row]
index_column = [row.index(element) for row in list_2d if element in row]
You can use the following sample method too:
data = [[1, 1,2],[12,4],[6]]
def m_array_index(arr, searchItem):
for i,x in enumerate(a):
for j,y in enumerate(x):
if y == searchItem:
return i,j
return -1,-1#not found
print m_array_index(data, 6)
Or with all occurrences(sure code could be optimized - modified to work with generators and so on - but here is just a sample):
occurrences = lambda arr, val: tuple((i,j) for i,x in enumerate(arr) for j,y in enumerate(x) if y == val) or ((-1,-1))
print occurrences(data, 1) # ((0, 0), (0, 1))
print occurrences(data, 12) # ((1, 0),)
print occurrences(data, 11) # (-1, -1)