Alphabetizing?
Erik Max Francis
max at alcyone.com
Sun Aug 24 19:45:54 EDT 2003
Lee Harr wrote:
> How about just sort()ing them?
>
> You can define a custom sort function if you want an
> alphabetical rather than lexical sort. ie:
...
> 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)
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:
>>> sort_alpha("where's", "whereas")
-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.maketrans
and S.translate.
--
Erik Max Francis && max at 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
More information about the Python-list
mailing list