[Numpy-discussion] Resending: List of indices

josef.pktd@gmai... josef.pktd@gmai...
Sun Apr 4 09:37:30 CDT 2010


On Sun, Apr 4, 2010 at 10:05 AM, Shailendra <shailendra.vikas@gmail.com> wrote:
> On Fri, Apr 2, 2010 at 12:34 PM, Shailendra <shailendra.vikas@gmail.com> wrote:
>> Hi All,
>>
>>>>> x=arange(10)
>>>>> indices=[(1,),(2,)]
>>>>> x[indices]
>> Traceback (most recent call last):
>>  File "<stdin>", line 1, in <module>
>> IndexError: unsupported iterator index

You are using two indices/coordinates in a 1d array

>>> np.ravel([(1,),(2,)])
array([1, 2])
>>> np.arange(5)[np.ravel([(1,),(2,)])]
array([1, 2])

>>> np.arange(5)[[1,2]]
array([1, 2])
>>> np.arange(5)[1,2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: invalid index

>>
>>
>> But following works.
>>>>> x=x.reshape(5,2)
>>>>> x
>> array([[0, 1],
>>       [2, 3],
>>       [4, 5],
>>       [6, 7],
>>       [8, 9]])
>>>>> indices=[(1,0),(0,1)]
>>>>> x[indices]
>> array([2, 1])

I don't know if it is clear in this example:

>>> np.arange(10).reshape(5,2)[(1,0,1),(0,1,1)]
array([2, 1, 3])

is equivalent to
>>> [np.arange(10).reshape(5,2)[i,j] for i,j in zip((1,0,1),(0,1,1))]
[2, 1, 3]

Josef
>>
>>
>> Should not the first one also work? How to convert [(1,),(2,)] to
>> [1,2] efficiently.
>>
>> Thanks,
>> Shailendra
>>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>


More information about the NumPy-Discussion mailing list