From: a on
On 13 May, 15:47, Stefan Behnel <stefan...(a)behnel.de> wrote:
> a, 13.05.2010 16:36:
>
> > this must be easy but its taken me a couple of hours already
>
> > i have
>
> > a=[2,3,3,4,5,6]
>
> > i want to know the indices where a==3 (ie 1 and 2)
>
>    indices = [ i for i,item in enumerate(a) if item == 3 ]
>
> > then i want to reference these in a
>
>    print [ a[i] for i in indices ]
>
> Stefan

thanks Stefan. very useful. I didn't get this from the python
documentation!
From: Tim Chase on
On 05/13/2010 09:36 AM, a wrote:
> this must be easy but its taken me a couple of hours already
>
> i have
>
> 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


From: a on
On 13 May, 16:19, Tim Chase <python.l...(a)tim.thechases.com> wrote:
> On 05/13/2010 09:36 AM, a wrote:
>
> > this must be easy but its taken me a couple of hours already
>
> > i have
>
> > 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.
From: Carey Tilden on
On Thu, May 13, 2010 at 8:45 AM, a <oxfordenergyservices(a)googlemail.com> wrote:
> On 13 May, 16:19, Tim Chase <python.l...(a)tim.thechases.com> wrote:
>> On 05/13/2010 09:36 AM, a wrote:
>>
>> > this must be easy but its taken me a couple of hours already
>>
>> > i have
>>
>> > 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.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Out of curiosity, why are you using two arrays? Have you considered a
dict? There are of course good reasons not to use a dict in this
situation, but you haven't said one way or another.

Carey
From: Tim Chase on
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