indexing lists/arrays question

Tim Chase python.list at tim.thechases.com
Thu May 13 13:18:39 EDT 2010


On 05/13/2010 10:45 AM, a wrote:
>>> a=[2,3,3,4,5,6]
>>
>>> i want to know the indices where a==3 (ie 1 and 2)
>>
>> indexes = [i for (i, v) in enumerate(a) where v==3]
>>
>>> then i want to reference these in a
>>
>> In a _what_?  You can then do things like
>>
>>     for i in indexes:
>>       print a[i]
>>
>> (but you already know these are "3", so it's not very exciting...)
>>
>> -tkc
>
> really its to get the indexes in 1 array where something equals
> something then reference these in another array.

If your two arrays are of the same length, you can do things like

   a = [2,3,3,4,5,6]
   b = ['a', 'b', 'c', 'd', 'e', 'f']

   print [m for (n,m) in zip(a,b) if n == 3]

and skip the indexes altogether.

-tkc







More information about the Python-list mailing list