Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 9th, 2007, 01:25 AM
John
Guest
 
Posts: n/a
Default any better code to initalize a list of lists?

For my code of radix sort, I need to initialize 256 buckets. My code looks a
little clumsy:

radix=[[]]
for i in range(255):
radix.append([])

any better code to initalize this list?


  #2  
Old March 9th, 2007, 01:35 AM
Paul Rubin
Guest
 
Posts: n/a
Default Re: any better code to initalize a list of lists?

"John" <rds1226@sh163.netwrites:
Quote:
For my code of radix sort, I need to initialize 256 buckets. My code looks a
little clumsy:
>
radix=[[]]
for i in range(255):
radix.append([])
>
any better code to initalize this list?
Typically you'd say
radix = [[] for i in xrange(256)]

but what are you really doing? This plan to implement radix sorting
sounds a little bit odd, unless it's just an exercise.

You could also consider using a dictionary instead of a list,
something like:

radix = defaultdict(list)
  #3  
Old March 9th, 2007, 02:15 AM
John
Guest
 
Posts: n/a
Default Re: any better code to initalize a list of lists?

I want to radix sort non-negative integers that can fit into 32-bits. That
will take 4 passes, one for each byte. So, I will need 256 "buckets"
The list radix of 256 elements of list is good for this purpose.

Your code is much shorter, works and probably takes less time.

Thanks!

John


"Paul Rubin" <http://phr.cx@NOSPAM.invalidwrote in message
news:7xlki7dx5m.fsf@ruckus.brouhaha.com...
Quote:
"John" <rds1226@sh163.netwrites:
Quote:
>For my code of radix sort, I need to initialize 256 buckets. My code
>looks a
>little clumsy:
>>
>radix=[[]]
>for i in range(255):
> radix.append([])
>>
>any better code to initalize this list?
>
Typically you'd say
radix = [[] for i in xrange(256)]
>
but what are you really doing? This plan to implement radix sorting
sounds a little bit odd, unless it's just an exercise.
>
You could also consider using a dictionary instead of a list,
something like:
>
radix = defaultdict(list)

  #4  
Old March 9th, 2007, 02:45 AM
Paul Rubin
Guest
 
Posts: n/a
Default Re: any better code to initalize a list of lists?

"John" <rds1226@sh163.netwrites:
Quote:
I want to radix sort non-negative integers that can fit into 32-bits.
But why do you want to do that? Why not just use Python's built-in
sorting operation?
  #5  
Old March 9th, 2007, 06:05 AM
Terry Reedy
Guest
 
Posts: n/a
Default Re: any better code to initalize a list of lists?


"Paul Rubin" <"http://phr.cx"@NOSPAM.invalidwrote in message
news:7xps7j6tct.fsf@ruckus.brouhaha.com...
| "John" <rds1226@sh163.netwrites:
| I want to radix sort non-negative integers that can fit into 32-bits.
|
| But why do you want to do that? Why not just use Python's built-in
| sorting operation?

Perhaps to learn or teach about the algorithm. Python is good for that.



  #6  
Old March 11th, 2007, 08:15 PM
Donald Fredkin
Guest
 
Posts: n/a
Default Re: any better code to initalize a list of lists?

John wrote:
Quote:
For my code of radix sort, I need to initialize 256 buckets. My code
looks a little clumsy:
>
radix=[[]]
for i in range(255):
radix.append([])
>
any better code to initalize this list?
radix = [[[]]*256][0]

--

  #7  
Old March 11th, 2007, 08:15 PM
Stargaming
Guest
 
Posts: n/a
Default Re: any better code to initalize a list of lists?

Donald Fredkin schrieb:
Quote:
John wrote:
>
>
Quote:
>>For my code of radix sort, I need to initialize 256 buckets. My code
>>looks a little clumsy:
>>
>>radix=[[]]
>>for i in range(255):
> radix.append([])
>>
>>any better code to initalize this list?
>
>
radix = [[[]]*256][0]
>
Quote:
Quote:
Quote:
>>x = [[[]]*256][0]
>>x
[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [],
[], [], ...
Quote:
Quote:
Quote:
>>x[0].append(1)
>>x
[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1],
[1], [1], ...

Nice one.
  #8  
Old March 11th, 2007, 08:15 PM
Bart Willems
Guest
 
Posts: n/a
Default Re: any better code to initalize a list of lists?

Donald Fredkin wrote:
Quote:
John wrote:
>
Quote:
>For my code of radix sort, I need to initialize 256 buckets. My code
>looks a little clumsy:
>>
>radix=[[]]
>for i in range(255):
> radix.append([])
>>
>any better code to initalize this list?
>
radix = [[[]]*256][0]
>
No I fell for that one too - it's the same as 'radix = [[]] * 256. Try
radix[0].append('dead parrot')..
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles