Alphabetizing?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tjland@iserv.net

    Alphabetizing?

    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!


    When you see the net take the shot
    When you miss your shot shoot again
    When you make your shot you win!

    Just remember
    Offense sells tickets
    But defense wins championships!

  • Lee Harr

    #2
    Re: Alphabetizing?

    In article <mailman.106175 8433.27108.pyth on-list@python.org > wrote:[color=blue]
    > 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!
    >
    >[/color]


    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('ente r word ("done" to finish) :')
    if word == 'done':
    break
    words.append(wo rd)

    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


    Comment

    • Erik Max Francis

      #3
      Re: Alphabetizing?

      Lee Harr wrote:
      [color=blue]
      > How about just sort()ing them?
      >
      > You can define a custom sort function if you want an
      > alphabetical rather than lexical sort. ie:[/color]
      ...[color=blue]
      > 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)[/color]

      You've got the right approach (sorting based on a signature derived from
      the string, rather than the string itself), although to be picky this
      isn't yet an alphabetic sort, since it should be stripping away
      non-alphabetic characters:
      [color=blue][color=green][color=darkred]
      >>> sort_alpha("whe re's", "whereas")[/color][/color][/color]
      -1

      Of course this starts to get into questions of what you consider a word
      or not, but a true alphabetic sort would remove non-alphabetic
      characters first. You can do this easily enough with string.maketran s
      and S.translate.

      --
      Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
      __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
      / \ An undevout astronomer is mad.
      \__/ Edward Young

      Comment

      Working...