0
import sys
def candidateWord():
   filePath = "sample.txt"
   file = open(filePath,'r')
   word_count = {}
   for line in sys.stdin.readlines():
         for word in line.split():
            #words = word.lower()
            words = word.strip('!,.?1234567890-=@#$%^&*()_+').lower()
            word_count[words] = word_count.get(words,0) + 1

         for key in word_count.keys():
            #sorted(word, key = str,lower)
            print (str(key)+' '+str(word_count[key]))

candidateWord()

How would I sort the words I have in a textfile by their frequency using what I have already?

The text file (sample.txt) contains the following: How are you How are you I am good. HBHJKOLDSA How

My desire output should be:

how 3
am 2
are 2
i 2
you 2
good 1
hbhjkoldsa 1

I am working in python 3.

4

1 回答 1

5

Use the collections.Counter:

from collections import Counter

with open("sample.txt", 'r') as f:
    text = f.read()

words = [w.strip('!,.?1234567890-=@#$%^&*()_+') for w in text.lower().split()]

counter = Counter(words)

print(counter.most_common())
# [('how', 3), ('are', 2), ('you', 2), ('good', 1), ('i', 1), ('am', 1), ('hbhjkoldsa', 1)]

Your desired output:

print("\n".join("{} {}".format(*p) for p in counter.most_common()))

 

Using your code and sorting by (frequency desc, word asc):

for key, value in sorted(word_count.items(), key=lambda p: (-p[1], p[0])):
    print("{} {}".format(key, value))

 

The Counter result can be sorted in the same way, just replace word_count.items() with counter.most_common().

于 2013-03-31T22:07:30.100 回答