indexing lists/arrays question
Neil Cerutti
neilc at norwich.edu
Thu May 13 11:05:46 EDT 2010
On 2010-05-13, a <oxfordenergyservices at 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. ***
More information about the Python-list
mailing list