From: a on
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)

then i want to reference these in a

ie what i would do in IDL is

b=where(a eq 3)
a1=a(b)

any ideas?

Thanks

From: Stefan Behnel on
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

From: Matthew Wilson on
On Thu 13 May 2010 10:36:58 AM EDT, 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)
>
> then i want to reference these in a
>
> ie what i would do in IDL is
>
> b=where(a eq 3)
> a1=a(b)


There's several solutions. Here's one:

It is a recipe for madness to use a list of integers and then talk
about the position of those integers, so I renamed your list to use
strings.

>>> a = ['two', 'three', 'three', 'four','five', 'six']

Now I'll use the enumerate function to iterate through each element and
get its position::

>>> for position, element in enumerate(a):
... print position, element
...
0 two
1 three
2 three
3 four
4 five
5 six

And now filter:

>>> for position, element in enumerate(a):
... if element == 'three':
... print position, element

1 three
2 three

And now do something different besides printing:

>>> b = []
>>> for position, element in enumerate(a):
... if element == 'three':
... b.append(position)

And now we can rewrite the whole thing from scratch to use a list
comprehension:

>>> [position for (position, element) in enumerate(a) if element == 'three']
[1, 2]

HTH

Matt

From: Neil Cerutti on
On 2010-05-13, a <oxfordenergyservices(a)googlemail.com> 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)
>
> then i want to reference these in a
>
> ie what i would do in IDL is
>
> b=where(a eq 3)
> a1=a(b)
>
> any ideas?

For a sorted sequence the bisect module is a good start.

>>> start = bisect.bisect_left(a, 3)
>>> end = bisect.bisect_right(a, 3, bs)
>>> b = list(range(start, end))
>>> b
[1, 2]

If the list isn't necessarily sorted, try filter and enumerate.

>>> b = [a for a,b in filter(lambda x: x[1]==3, enumerate(a))]
>>> b
[1, 2]

--
Neil Cerutti
*** Your child was bitten by a Bat-Lizard. ***
From: Neil Cerutti on
On 2010-05-13, Stefan Behnel <stefan_ml(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 ]

That form of list comprehension is preferable to my use of filter
posted elsewhere, but it didn't occur to me. Oops!

--
Neil Cerutti
*** Your child was bitten by a Bat-Lizard. ***