any better code to initalize a list of lists?

John rds1226 at sh163.net
Thu Mar 8 21:05:01 EST 2007


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.invalid> wrote in message 
news:7xlki7dx5m.fsf at ruckus.brouhaha.com...
> "John" <rds1226 at sh163.net> writes:
>> 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) 





More information about the Python-list mailing list