From: a on 13 May 2010 13:48 On 13 May, 17:41, Carey Tilden <carey.til...(a)gmail.com> wrote: > On Thu, May 13, 2010 at 8:45 AM, a <oxfordenergyservi...(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/pythonlist > > 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 nonproprietary 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!
From: a on 13 May 2010 13:51 On 13 May, 18:18, Tim Chase <python.l...(a)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!
From: Tim Chase on 13 May 2010 20:18 On 05/13/2010 12:51 PM, a wrote: >> 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: >>> print zip(a,b) # using the abovedefined a/b [(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
First

Prev

Pages: 1 2 3 Prev: jython and emacs on windows Next: use only files but ignore directories on Windows 