python - How to find the index of an array within an array -


i have created array in way shown below; represents 3 pairs of co-ordinates. issue don't seem able find index of particular pair of co-ordinates within array.

import numpy 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 returned:

attributeerror: 'numpy.ndarray' object has no attribute 'index' 

the reason i'm trying can extend list, index of co-ordinate pair, within for-loop.

e.g.

v = [] in r: v.append(r.index(a))  

i'm not sure why index function isn't working, , can't seem find way around it.

i'm new programming excuse me if seems nonsense.

you can achieve desired result converting inner arrays (the coordinates) tuples.

r = map(lambda x: (x), r); 

and can find index of tuple using r.index((number1, number2));

hope helps!

[edit] explain what's going on in code above, map function goes through (iterates) items in array r, , each 1 replaces return result of lambda function. it's equivalent along these lines:

def somefunction(x):   return (x)  x in range(0, len(r)):   r[x] = somefunction(r[x]) 

so takes each item , it, putting in list. realized may not thought did (returning (x) doesn't seem change regular array tuple), situation because think iterating through python might create regular array out of numpy array.

to convert tuple, following code should work

r = map(tuple, r)  

(credits https://stackoverflow.com/a/10016379/2612012)


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -