[Tutor] index of elements in numpy array
bala.biophysics at gmail.com
Fri Sep 14 10:36:08 CEST 2012
Thank you all for the answer. Below, i have pasted a sample code that
shows what i am intending to do. The code fails at line 13 as numpy
array dnt have a index attribute.
1 #!/usr/bin/env python
2 import numpy as np
10 # loop over each row
11 for i1, d1 in enumerate(b):
12 # each x in d1 - value in z corresponding to index of x in d1
13 d1=[x-z[d1.index(x)] for x in d1]
If d1 is a simple list, i can fetch the index of its element as
d1.index(x). So i would like to know how can achieve the same with
Thanks once again,
On Fri, Sep 14, 2012 at 12:39 AM, Dave Angel <d at davea.name> wrote:
> On 09/13/2012 04:16 PM, Bala subramanian wrote:
>> I have a 2D matrix (a numpy array) and i am looping over each row in
>> the matrix. I would like to know how i can find the index of each
>> element in a while looping a row. Is there any function in numpy.
> Perhaps you're asking a more general question. When iterating over a
> collection, sometimes you not only want the object, but you also want
> the index you might have used to fetch it.
> for row in rows:
> dosomething with row
> for index, row in enumerate(rows):
> dosomething with row and with index
> Now if rows be a list, or whatever numpy decides to call it, then you
> can manipulate the particular one with rows[index].
More information about the Tutor