Say I have an array of values nameArr = ['josh','is','a','person'] and I want a function like arrayLocation(nameArr,['a','is']) to return [2 1].
Does that function already exist, if not how can I implement it efficiently?
Lists have a index method that you can use.
>>> nameArr = ['josh','is','a','person']
>>> # Using map
>>> map(nameArr.index, ['a', 'is'])
[2, 1]
>>> # Using list comprehensions
>>> [nameArr.index(x) for x in ['a', 'is']]
[2, 1]
BTW, index raises ValueError if the element is not in the list. So if you want to supply elements that are not in the list to the index method, you may need to handle the error appropriately.
If the array is large, and do many times of location, you can create a dict that map the value to it's index first:
d = dict(zip(nameArr, range(len(nameArr))))
items = ['a','is']
print [d.get(x, None) for x in items]