For this program I am trying to figure out how to sort/print the words out based on frequency of appearance in ascending order. As of now I can only get the words to sort/print based on alaphabetical order

from collections import Counter 
import string


while True:
    filename=raw_input('Enter a file name: ')
    if filename == 'exit':
        break
    try:
        file = open(filename, 'r') 
        text=file.read() 
        file.close() 
    except:
        print('file does not exist')
    else:

        for word in string.punctuation:
            text=text.replace(word, "")
        word_list = text.lower().split(None)
        word_freq = {}
        
        for word in word_list:
            word_freq[word] = word_freq.get(word, 0) + 1

        keys = sorted(word_freq.keys())
        for word in keys:
            if len(word) > 1:
                print (word, word_freq[word])//the words print out based on alphabetic order
                //how would I get them to print based on freqeuncy in ascending order?
          
  
            
print('Bye')

Recommended Answers

All 3 Replies

You can use operator.itemgetter() as shown in this example ...

# count words in a text and show them by decreasing frequency 
# using operator.itemgetter()
# collections.Counter() needs Python27 or Python3 

import collections
import operator

# sample text for testing (could come from a text file)
text = """\
If you see a turn signal blinking on a car with a southern license plate, 
you may rest assured that it was on when the car was purchased. 
"""

# create a word list of the words in text 
# remove given punctuation marks and change to lower case    
word_list = [word.strip('.,!?:;"').lower() for word in text.split()]

#print(word_list)  # test

cnt = collections.Counter()
for word in word_list:
    cnt[word] += 1

#print(cnt)  # test

print("The most frequent words are:")
# operator.itemgetter(1) implies v (frequency)
freq = operator.itemgetter(1)
for k, v in sorted(cnt.items(), reverse=True, key=freq):
    print("%3d  %s" % (v, k))

""" result (Python 2.7) >>>
The most frequent words are:
  3  a
  2  you
  2  was
  2  on
  2  car
  1  it
  1  rest
  1  see
  1  purchased
  1  if
  1  when
  1  plate
  1  assured
  1  that
  1  may
  1  southern
  1  blinking
  1  with
  1  license
  1  signal
  1  turn
  1  the
"""
commented: Great +1

Just a note collections Counter will always sort in descending count order.
Here i also show most_common(),no we now that all word after 'it' has 1 as count.

from collections import Counter
from string import punctuation

text = """\
If you see a turn signal blinking on a car with a southern license plate,
you may rest assured that it was on when the car car car car car was purchased.
"""

text = ''.join([c for c in text if c not in punctuation])
print Counter(text.split()).most_common(6)
#-->[('car', 6), ('a', 3), ('you', 2), ('was', 2), ('on', 2), ('it', 1)]
commented: nice +13
commented: good code +6
commented: Good one. +1

Just a note collections Counter will always sort in descending count order.
Here i also show most_common(),no we now that all word after 'it' has 1 as count.

from collections import Counter
from string import punctuation

text = """\
If you see a turn signal blinking on a car with a southern license plate,
you may rest assured that it was on when the car car car car car was purchased.
"""

text = ''.join([c for c in text if c not in punctuation])
print Counter(text.split()).most_common(6)
#-->[('car', 6), ('a', 3), ('you', 2), ('was', 2), ('on', 2), ('it', 1)]

Beautiful code but for me it is a blemish that you do list comprehension instead of generator in

text = ''.join([c for c in text if c not in punctuation])
text = ''.join(c for c in text if c not in punctuation)

works without preparing needless list and looks more beautiful.

You might find interesting also my post
lowercase word generator

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.