How to find the index of an array within an array
arrays, numpy, python
Solution
You can achieve the desired result by converting your inner arrays (the coordinates) to tuples.
R = map(lambda x: (x), R);
And then you can find the index of a tuple using R.index((number1, number2));
Hope this helps!
[Edit] To explain what's going on in the code above, the map function goes through (iterates) the items in the array R, and for each one replaces it with the return result of the lambda function. So it's equivalent to something along these lines:
def someFunction(x):
return (x)
for x in range(0, len(R)):
R[x] = someFunction(R[x])
So it takes each item and does something to it, putting it back in the list. I realized that it may not actually do what I thought it did (returning (x) doesn't seem to change a regular array to a tuple), but it does help your situation because I think by iterating through it python might create a regular array out of the numpy array.
To actually convert to a tuple, the following code should work
R = map(tuple, R)
(credits to https://stackoverflow.com/a/10016379/2612012)
Problem
I have created an array in the way shown below; which represents 3 pairs of co-ordinates. My issue is I don't seem to be able to find the index of a particular pair of co-ordinates within the array. ``` import numpy as np R = np.random.uniform(size=(3,2)) R Out[5]: array([[ 0.57150157, 0.46611662], [ 0.37897719, 0.77653461], [ 0.73994281, 0.7816987 ]]) R.index([ 0.57150157, 0.46611662]) ``` The following is returned: ``` AttributeError: 'numpy.ndarray' object has no attribute 'index' ``` The reason I'm trying to do this is so I can extend a list, with the index of a co-ordinate pair, within a for-loop. e.g. ``` v = [] for A in R: v.append(R.index(A)) ``` I'm just not sure why the index function isn't working, and can't seem to find a way around it. I'm new to programming so excuse me if this seems like nonsense.