简体   繁体   中英

How would I alphabetically sort a (very long) list of objects using radix sort in Python?

I am trying to sort lists within a list in alphabetical order using radix sort. I need the lists to be sorted by one of the attributes of an object I've created.

Note: I cannot use built-in sorts - I have to write my own. I was not allowed to use defaultdict so I used lists.

I have a list called results[]. In results[x] for all of results[] I have another list containing words of length x. Those words are stored as word objects containing the original word (originalWord), the alphabetized word (azWord), and its length (wLength). eg dog, dgo, 3.

Since I have many, many words, I've decided the radix sort would be most efficient for my purposes. I am relatively new to Python so I'm having some trouble writing the code correctly. I have a rough outline of what it should look like but I would appreciate help with fixing the syntax.

I am planning on using radix_sort in a for loop that iterates through results[]. I have a variable, maxL, that stores the longest word I have (aka the number of lists in results).

for x in range(0,maxL):
    radix_sort(results[x], x)

Here is my attempt at writing radix sort for strings. Please note that the azWord attribute is represented as a char list.

def radix_sort(List, length):
    buckets = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
    for i in range (0, length-1):    #for every letter "column"
        for word in List:    #for every word 
            index = ord(word[i].azWord)-ord('a')   #get the index of the word
            buckets[index].append(word)     #add word object to correct bucket
    for containedList in buckets:
        while(containedList):
            #I'm having trouble here with emptying the lists back into the bins

EDIT: Also, since I don't want to run out of memory (this is being done for a very long list of words), should I be clearing some things as I go that I don't need?

Also, currently, Eclipse is giving me an error "Expected:: Expected::" for this line:

for i in range (0, length-1)

Current version:

def radix_sort(List, length):
    buckets = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
    for i in range (length-1, -1, -1):    #for every letter "column"
        for word in List:    #for every word 
            index = ord(word.azWord[i])-ord('a')   #get the index of the word
            buckets[index].append(word)     #add word object to correct bucket
    List[:] = []
    for containedList in buckets:
        List.extend(containedList)

I think you're missing some colons on these lines:

for i in range (0, length-1)  # Need a colon here

for word in List  # Need a colon here

for containedList[] in buckets  # Need a colon here

while(containedList[])  # Need a colon here

To put the sorted results back into the list:

List[:] = []
for containedList in buckets:
    List.extend(containedList)

One more thing, you'll need to sort from least significant to most significant if you expect the proper sort:

for i in range(length-1, -1, -1):

Note that your original range was incorrect anyway, the end point isn't included in the range so stopping at length-1 was going to skip the last letter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM