"Newbie" questions - "unique" sorting ?

Kim Petersen kp at kyborg.dk
Tue Jun 24 03:52:34 EDT 2003


Cousin Stanley wrote:
> John ....
> 
> { 1. Good News | 2. Bad News | 3. Good News } ....
> 
>   1. Good News ....
> 
>      The last version of word_list.py that I up-loaded
>      works as expected with your input file producing
>      an indexed word list with no duplicates ...
> 
> 
>   2. Bad News ....
> 
>      It was  S L O W E R  than the proverbial turtle
>      in the tar pit ...
> 
How about the simple approach?

total=0
unique={}
for line in open(infile,"rb").xreadlines():
    for word in line.strip().split():
        unique[word]=unique.get(word,0)+1
        total+=1
uwords=unique.keys()
uwords.sort()
open(outfile,"wb").write('\n'.join(uwords))

-- 
Med Venlig Hilsen / Regards

Kim Petersen - Kyborg A/S (Udvikling)
IT - Innovationshuset
Havneparken 2
7100 Vejle
Tlf. +4576408183 || Fax. +4576408188





More information about the Python-list mailing list