2

I've had a look at other similar questions already but haven't been able to apply the answers to my program. At the moment the frequencies are printed in ascending order, what do I change to make it print it in descending order?

from sys import argv
frequencies = {}
for ch in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
   frequencies[ch] = 0

for filename in argv[1:]:
    try:
        f = open(filename)
    except IOError:
        print 'skipping unopenable', filename
        continue

 text = f.read()                
 f.close()                      

 for ch in text:                
     if ch.isalpha():
         ch = ch.upper()
         frequencies[ch] = frequencies[ch] + 1

for ch in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
    print ch, frequencies[ch]

Thanks in advance.

4 Answers4

5

You don't have to reinvent the wheel. Use standard library features:

from sys import argv
from collections import Counter

frequencies = Counter()

for filename in argv[1:]:
    with open(filename) as f:
        text = f.read()
    frequencies.update(ch.upper() for ch in text if ch.isalpha())

for ch, freq in frequencies.most_common():
    print ch, freq
eumiro
  • 207,213
  • 34
  • 299
  • 261
2

You can call items on the dict to get a list of tuples of the items in the dictionary. You can then (reverse) sort by the second item in the tuple (the value in the dict, the frequency):

sorted(frequencies.items(), key=lambda x: -x[1])

By the way, rather than using 'ABCD..., you can use string.ascii_uppercase.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • Thanks but bit confused. Where do I add item and dict? – user1390700 May 12 '12 at 06:35
  • @user1390700: `frequencies` is a `dict`. `items` is a method on `dict`s. Basically, rather than outputting with `for ch in 'ABC...':` `print ch, frequencies[ch]`, you can use `for ch, freq in sorted(...):` `print ch, freq` – icktoofay May 12 '12 at 06:40
  • Just wondering if I wanted to change to find the frequencies of words rather than letters what would I change in the coding? – user1390700 May 12 '12 at 08:00
  • @user1390700: If you used a `Counter` as suggested in [eumiro's answer](http://stackoverflow.com/a/10561660), then you could get a list of words with `text.split()` (although that would include punctuation in the words) or a regular expression like `\w+` (which would not). Then just loop over those words and count as you would the letter. (i.e. `frequencies.update(word)`) – icktoofay May 12 '12 at 19:20
  • @user1390700: If you don't want to use a `Counter`, then a `defaultdict` might work. If you don't want to use either, then you'd need to check if the word is in the dictionary first, and if it isn't, set it to 1; if it is, increment it. (basically what a `Counter` would do) – icktoofay May 12 '12 at 19:21
  • Thanks i shall give it a go. Cheers – user1390700 May 13 '12 at 07:55
0

Descending from Z to A? Change the string constant on the second last line to 'ZYXWV...A'.

Antti Huima
  • 25,136
  • 3
  • 52
  • 71
0
from sys import argv
tre="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

for filename in argv[1:]:
    with open(filename) as f:
        text = f.read()
        ttt=list(set(zip(tre,map(text.count,tre))))
        ttt1=sorted([[x[1],x[0]] for x in ttt])
        ttt1.reverse()
        ttt3=[[x[1],x[0]] for x in ttt1]
        for x in ttt3:
            print x[0],x[1]
namit
  • 6,780
  • 4
  • 35
  • 41