Alphabetizing?

Lee Harr missive at frontiernet.net
Sun Aug 24 18:33:10 EDT 2003


In article <mailman.1061758433.27108.python-list at python.org> wrote:
> Ok, im trying to creat a small simple program that alphabetizes user
> inputed words. I thought why not just create a def that splits all the
> words down into char's then take the first char and crosscheck it with all
> lowercase letters. Then find its position and return the results to the
> user in order or 1-26! But this takes a long time and the code gets
> confusing. Any suggestion? Thanx in advance!
> 
> 


How about just sort()ing them?

You can define a custom sort function if you want an
alphabetical rather than lexical sort. ie:


words = []
while 1:
    word = raw_input('enter word ("done" to finish) :')
    if word == 'done':
        break
    words.append(word)

def sort_alpha(a, b):
    if a.lower() < b.lower():
        return -1
    elif a.lower() > b.lower():
        return 1
    else:
        return 0

words.sort(sort_alpha)

print words






More information about the Python-list mailing list