any better code to initalize a list of lists?

J

John

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?
 
P

Paul Rubin

John said:
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)
 
J

John

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 said:
John said:
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)
 
P

Paul Rubin

John said:
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?
 
T

Terry Reedy

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

Donald Fredkin

John said:
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]

--
 
S

Stargaming

Donald said:
John wrote:

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]
>>> x = [[[]]*256][0]
>>> x
[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [],
[], [], ...
[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1],
[1], [1], ...

Nice one.
 
B

Bart Willems

Donald said:
John said:
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')..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top