3

I have an array

v = (x,y,z)

and two multidimensional array

l = (a,b,c),(d,e,f)

and

r = (g,h,i),(l,m,n),(x,y,z).

I want to know the index of v no matter if is in the first or second multidimensional array. I tried numpy.where(v==l)[0][0] but it returns:

Index 0 is out of bounds for axis 0 with size 0.

Works only if I know before the matrix where I have to search the Index, but I don't. Thanks

And If I want to know the index and the array that contains it?

3
  • 1
    What is your actual data like? Are you talking about tuples, Python lists or NumPy arrays? Commented Jan 10, 2014 at 17:42
  • What do you want as output here? By returning just index how would you know in which array you found it? Commented Jan 10, 2014 at 17:43
  • I'm talking of np.arrays that could be huges Commented Jan 10, 2014 at 17:46

2 Answers 2

1

Define a function that accepts the item to be searched and the list of array to be searched in and use a loop to find that item in each array. Use exception handling to catch the IndexError.

>>> import numpy as np
>>> v = np.array([[1, 2, 3]])
>>> r = np.array([[1, 2, 3], [0, 9, 8], [2, 4, 4]])
>>> l = np.array([[4, 5, 6], [7, 8, 9]])
def get_index(seq, *arrays):
    for array in arrays:
        try:
            return np.where(array==seq)[0][0]
        except IndexError:
            pass
...         
>>> get_index(v, r, l)
0
>>> get_index(np.array([7, 8, 9]), r, l)
1

You'd get None as output if the item is not found in any of the array.

Update:

If you want the name as well then pass the arrays in a dictionary:

 def get_index(seq, **arrays):
    for name, array in arrays.items():
        try:
            return name, np.where(array==seq)[0][0]
        except IndexError:
            pass
...         
>>> get_index(v, **dict(r=r, l=l))
('r', 0)
>>> get_index(np.array([7, 8, 9]), **dict(r=r, l=l))
('l', 1)
Sign up to request clarification or add additional context in comments.

5 Comments

Ashwini if I want to know also in wich matrix is r or l?
@user3154159 Their names or just their value?
The name r or l and index of v in r or l
@user3154159 AFAIK numpy arrays don't have any __name__ attribute, so you'll have to pass in a dict. Check out the updated solution.
It Works very well. You're amazing. Thank you
1
def containsArray(v):
    for index, arr in enumerate(l):
        if v == arr:
            ret index;

    for index, arr in enumerate(r):
        if v == arr:
            ret index;

4 Comments

It works, but it's not Pythonic to iterate over list with index. Please, don't write C in Python - it's ugly!
I don't know if the v array is in l or r
I need the index of v in r or l
@user3154159 then separate the for loops into 2 different functions and call them appropriately

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.