
March 9th, 2007, 01:25 AM
| | | 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? | 
March 9th, 2007, 01:35 AM
| | | 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) | 
March 9th, 2007, 02:15 AM
| | | 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)
| | 
March 9th, 2007, 02:45 AM
| | | 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? | 
March 9th, 2007, 06:05 AM
| | | 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. | 
March 11th, 2007, 08:15 PM
| | | 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]
-- | 
March 11th, 2007, 08:15 PM
| | | 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]
>
| [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [],
[], [], ... [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1],
[1], [1], ...
Nice one. | 
March 11th, 2007, 08:15 PM
| | | 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').. |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | 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.
|