473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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?
Mar 9 '07 #1
7 1179
"John" <rd*****@sh163.netwrites:
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)
Mar 9 '07 #2
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://ph****@NOSPAM.invalidwrote in message
news:7x************@ruckus.brouhaha.com...
"John" <rd*****@sh163.netwrites:
>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)

Mar 9 '07 #3
"John" <rd*****@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?
Mar 9 '07 #4

"Paul Rubin" <"http://phr.cx"@NOSPAM.invalidwrote in message
news:7x************@ruckus.brouhaha.com...
| "John" <rd*****@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.

Mar 9 '07 #5
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]

--

Mar 11 '07 #6
Donald Fredkin schrieb:
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
[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [],
[], [], ...
>>x[0].append(1)
x
[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1],
[1], [1], ...

Nice one.
Mar 11 '07 #7
Donald Fredkin wrote:
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]
No I fell for that one too - it's the same as 'radix = [[]] * 256. Try
radix[0].append('dead parrot')..
Mar 11 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
12
by: Jonathan Bartlett | last post by:
Just finished a new IBM DeveloperWorks article on linked lists, and thought you all might be interested. It's not an introduction -- it instead covers some of the more interesting aspects of...
24
by: markscala | last post by:
Problem: You have a list of unknown length, such as this: list = . You want to extract all and only the X's. You know the X's are all up front and you know that the item after the last X is...
7
by: fidtz | last post by:
The code below works fine, but it is less than nice to look at and somewhat long winded. Is there a better way to do the docstring task? This is the first working version, and can proabably be...
22
by: craig | last post by:
I've declared the following as a "vector" of "lists" using STL vector< list<Edge*> > I've tried many ways to add objects to this but can't figure out how to do it. Does anyone have any tips,...
2
by: Cox | last post by:
Hello: My address jsmith435@cox.net is subscribed to at least the PHP General mailing list. I have for days now been trying to unsubscribe from all PHP mail lists. I have followed the...
17
by: anyone.anon | last post by:
Let p=malloc(N) for some N>0. As far as I understand it, free(p+k) for 0<k<N causes undefined behavior, since only a pointer returned by (m| re|c)alloc() can validly be passed to free(). This...
19
by: Alexandre Badez | last post by:
I'm just wondering, if I could write a in a "better" way this code lMandatory = lOptional = for arg in cls.dArguments: if arg is True: lMandatory.append(arg) else: lOptional.append(arg)...
1
by: anonymous | last post by:
I have written a simple code using inline assembly to compare the individual bytes of two lists (X and Y) and to store the higher byte of each list in a third list (LARGER). The code is as follows...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.