Re: [Numpy-discussion] For loop tips

2006-08-31 Thread Torgil Svensson
def list2index(L): uL=sorted(set(L)) idx=dict((y,x) for x,y in enumerate(uL)) return uL,asmatrix(fromiter((idx[x] for x in L),dtype=int,count=len(L))) adding the count will save you a little more time, and temporary memory [see related thread]. //Torgil On 8/29/06, Keith Goodman <[EM

Re: [Numpy-discussion] For loop tips

2006-08-29 Thread Keith Goodman
On 8/29/06, Torgil Svensson <[EMAIL PROTECTED]> wrote: > something like this? > > def list2index(L): >uL=sorted(set(L)) >idx=dict((y,x) for x,y in enumerate(uL)) >return uL,asmatrix(fromiter((idx[x] for x in L),dtype=int)) Wow. That's amazing. Thank you. --

Re: [Numpy-discussion] For loop tips

2006-08-29 Thread Torgil Svensson
something like this? def list2index(L): uL=sorted(set(L)) idx=dict((y,x) for x,y in enumerate(uL)) return uL,asmatrix(fromiter((idx[x] for x in L),dtype=int)) //Torgil On 8/29/06, Keith Goodman <[EMAIL PROTECTED]> wrote: > On 8/29/06, Tim Hochberg <[EMAIL PROTECTED]> wrote: > > Keith Go

Re: [Numpy-discussion] For loop tips

2006-08-29 Thread Keith Goodman
On 8/29/06, Tim Hochberg <[EMAIL PROTECTED]> 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 whe

Re: [Numpy-discussion] For loop tips

2006-08-29 Thread Alan G Isaac
You can get some speed up for numeric data: def list2index2(L): aL = asarray(L) eL = empty_like(L) for v,k in enumerate(set(L)): eL[aL == k] = v return numpy.asmatrix(eL).T fwiw, Alan Isaac - Using Tomcat but

Re: [Numpy-discussion] For loop tips

2006-08-29 Thread Torgil Svensson
def list2index(L): idx=dict((y,x) for x,y in enumerate(set(L))) return asmatrix(fromiter((idx[x] for x in L),dtype=int)) # old $ python test.py Numbers: 29.4062280655 seconds Characters: 84.6239070892 seconds Dates: 117.560418844 seconds # new $ python test.py Numbers: 1.79700994492 secon

Re: [Numpy-discussion] For loop tips

2006-08-29 Thread Tim Hochberg
Tim Hochberg 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

Re: [Numpy-discussion] For loop tips

2006-08-29 Thread Tim Hochberg
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 i

[Numpy-discussion] For loop tips

2006-08-29 Thread Keith Goodman
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. I've