Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > efficient list reduction

Reply
Thread Tools

efficient list reduction

 
 
A B Carter
Guest
Posts: n/a
 
      05-11-2004
I have two lists. The values of the second list can be viewed as keys
for the first. I want to create a newlist based on the implied
mapping. The straight Python code would be:

newlist=[]
for key in keys:
newlist.append(oldlist[key])

What's the most efficient way of doing this? The best I could do was
the following list comprehension:

[oldlist[key] for key in keys]

Have I missed something?

Regards, A B Carter
 
Reply With Quote
 
 
 
 
Carl Banks
Guest
Posts: n/a
 
      05-11-2004
A B Carter wrote:
>
>
> I have two lists. The values of the second list can be viewed as keys
> for the first. I want to create a newlist based on the implied
> mapping. The straight Python code would be:
>
> newlist=[]
> for key in keys:
> newlist.append(oldlist[key])
>
> What's the most efficient way of doing this? The best I could do was
> the following list comprehension:
>
> [oldlist[key] for key in keys]
>
> Have I missed something?



newlist = map(oldlist.__getitem__,keys)

Quite a bit faster for me.


--
CARL BANKS http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work."
-- Parody of Mr. T from a Robert Smigel Cartoon
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      05-11-2004
(A B Carter) writes:
> [oldlist[key] for key in keys]
>
> Have I missed something?


If the keys and values are all characters or small positive ints,
maybe you can contort your program to let you use the very fast
string.translate operation. Otherwise, use psyco, and if your
listcomp is still not fast enough, write a C extension.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Efficient creation of "refined" list<T>, from old list<T> janzon@gmail.com C++ 3 10-12-2006 06:36 PM
Nodeset Difference AKA Nodeset Reduction Bryan Galvin XML 2 09-29-2004 12:09 PM
NodeSet Reduction Bryan Galvin XML 3 06-14-2004 09:56 AM
Bus reduction Patrick VHDL 1 06-07-2004 01:46 PM
AVI Size Reduction Martin Knight Computer Support 3 11-03-2003 07:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57