Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the index of an array within an array

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.

like image 366
user3258480 Avatar asked Jan 31 '14 19:01

user3258480


3 Answers

index() is a method of the type list, not of numpy.array. Try:

R.tolist().index(x)

Where x is, for example, the third entry of R. This first convert your array into a list, then you can use index ;)

like image 81
altroware Avatar answered Sep 23 '22 13:09

altroware


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)

like image 33
Clint Powell Avatar answered Sep 24 '22 13:09

Clint Powell


Numpy arrays don't an index function, for a number of reasons. However, I think you're wanting something different.

For example, the code you mentioned:

v = []
for A in R:
    v.append(R.index(A))

Would just be (assuming R has unique rows, for the moment):

v = range(len(R))

However, I think you might be wanting the built-in function enumerate. E.g.

for i, row in enumerate(R):
    # Presumably you're doing something else with "row"...
    v.append(i)

For example, let's say we wanted to know the indies where the sum of each row was greater than 1.

One way to do this would be:

v = []
for i, row in enumerate(R)
    if sum(row) > 1:
        v.append(i)

However, numpy also provides other ways of doing this, if you're working with numpy arrays. For example, the equivalent to the code above would be:

v, = np.where(R.sum(axis=1) > 1)

If you're just getting started with python, focus on understanding the first example before worry too much about the best way to do things with numpy. Just be aware that numpy arrays behave very differently than lists.

like image 23
Joe Kington Avatar answered Sep 24 '22 13:09

Joe Kington