indexing lists/arrays question

a oxfordenergyservices at googlemail.com
Thu May 13 13:51:13 EDT 2010


On 13 May, 18:18, Tim Chase <python.l... at tim.thechases.com> wrote:
> 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

mmm, that's clever, thanks.  although i don't know why it works yet.
at least i found a good user group!



More information about the Python-list mailing list