[Numpy-discussion] For loop tips

Torgil Svensson torgil.svensson at gmail.com
Tue Aug 29 14:44:11 CDT 2006

something like this?

def list2index(L):
   idx=dict((y,x) for x,y in enumerate(uL))
   return uL,asmatrix(fromiter((idx[x] for x in L),dtype=int))


On 8/29/06, Keith Goodman <kwgoodman at gmail.com> wrote:
> On 8/29/06, Tim Hochberg <tim.hochberg at ieee.org> wrote:
> > Keith Goodman wrote:
> > > I have a very long list that contains many repeated elements. The
> > > elements of the list can be either all numbers, or all strings, or all
> > > dates [datetime.date].
> > >
> > > I want to convert the list into a matrix where each unique element of
> > > the list is assigned a consecutive integer starting from zero.
> > >
> > If what you want is that the first unique element get's zero, the second
> > one, I don't think the code below will work in general since the dict
> > does not preserve order. You might want to look at the results for the
> > character case to see what I mean. If you're looking for something else,
> > you'll need to elaborate a bit. Since list2index doesn't return
> > anything, it's not entirely clear what the answer consists of. Just idx?
> > Idx plus uL?
> The output I wanted (in my mind, but unfortunately not in my previous
> email) is idx and uL where uL[0] corresponds to the zeros in idx,
> uL[1] corresponds to the ones in idx. etc.
> I'd also like the uL's to be ordered (now I see that characters and
> dates aren't ordered, ooops, thanks for telling me about that). Or
> optionally ordered by a second list input which if present would be
> used instead of the unique values of L.
> Thank you all for the huge improvements to my code. I'll learn a lot
> studying all of them.
> -------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion

More information about the Numpy-discussion mailing list