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