indexing lists/arrays question

A

a

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
 
S

Stefan Behnel

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 for i in indices ]

Stefan
 
M

Matthew Wilson

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::
... print position, element
...
0 two
1 three
2 three
3 four
4 five
5 six

And now filter:
... 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
 
N

Neil Cerutti

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.
[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]
 
N

Neil Cerutti

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!
 
T

Tim Chase

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

(but you already know these are "3", so it's not very exciting...)

-tkc
 
A

a

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

(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.
 
C

Carey Tilden

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

(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.


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
 
T

Tim Chase

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

(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
 
A

a

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
(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.

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


i am reading a 2,n array from one website, column1=times,
column2=values
then another 2,n array from another website, column1=times,
column2=values

the times are different but may (or may not) coincide in places

i need to make a 3rd array, 2,n where the first column are column2
values from array1 and the second column are column2 values from array
2 #where the timestamps agree#

i'm an idl programmer and doing this would be second nature but i need
to make an application which does something along the lines of the
above then plots column2 vs column2 for the above array3 (plus some
other maths). needs to be non-proprietary and work on different
platforms. i just started with python, not even sure if it's the best
thing. the plotting routines seem not to come wrapped with standard
python which is a bit of a pain.

the routine i use most in idl is 'where' and though i managed to write
a def which worked, i couldn't then dereference the list of those
indexes. i'm a bit old to be learning new languages

thanks for your help!
 
A

a

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
(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!
 
T

Tim Chase

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.

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

the zip() function takes its parameters and returns a list
containing paired items from each list:
[(2, 'a'), (3, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f')]

The list comprehension then iterates over the elements of that
list, assigned as (n,m), testing "n" (the numeric value you want
to test) and returning "m" (the corresponding value in "b")

-tkc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top