hi guy
just started python, i have a text here and want wo find the number of each letters, and sort the number from the most frequent to the least frequent.

hope you guys can hepe me

here is the text, just some random letters,

acbbmnhctrgnmmxfnfbmqrhnchfwcqwtacvtfhmecttvfcnvphchmpgdmebjdcqwhdfnfrcawhdfrkanahtcjaqxahpkmqdardfcnhcqwjmprdcttvfpkdftwaqbmnfhdcqhdarcrhdfzmnwrzfnfrkmsfqhdfjkcrrfwhdnmpxdhdfzcttcqwrhmmwpkmqcqmkfqgmpqhnjnmcwzahdeaftwrmqfahdfndcqwhdfgahjdcwfqhanftjlcqardfwqmhclfrhaxfmeahzcrhmvfrffqhdfwcnsqfrrcqwhdfbarhdcwlcqardfwzahdahemnahzcrcgtfcngmtwzaqhfnwcjzahdrqmzpkmqhdfxnmpqwxmmwdfclfqrcawrgnmmxfgtcrkaqxdar

Use dictionary or (defaultdict or Counter) from collections module if you are using recent version of Python. Post your code with code-tags and error messages for further advice.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

i was trying to use the .count() command but it wasnt working
i tried to put the letter in a = acbbmnhctrgnmmxfnf....
a.count(a) is not working in this case, i think it needs a "," between these letters.

this is what i did
this give me the sorted letter, but after this, i want to sort the numbers, couldnt find a way to do that.

and in this case it the letter is not appear in text1, and in the output it doesnt show the letter, any idea how to show the letter and equal to 0? (something like "i = 0", if i is not in text1)

text1 = acbbmnhctrgnmmxfnf.....
letterCount = {}
for char in text1:
letterCount[char] = letterCount.get(char, 0) + 1

sortedCharTuples = sorted(letterCount.items())


print "Character count in alphabetical order:"

for charTuple in sortedCharTuples:

print "%s = %d" % (charTuple[0], charTuple[1])

print charTuple

You did not follow advice on code tags, I put this time, but next time include code tags.

text1 = acbbmnhctrgnmmxfnf.....
letterCount = {}
for char in text1:
    letterCount[char] = letterCount.get(char, 0) + 1
    
sortedCharTuples = sorted(letterCount.items())


print "Character count in alphabetical order:"

for charTuple in sortedCharTuples:
    
    print "%s = %d" % (charTuple[0], charTuple[1]) 

print charTuple

Here is sorting for second value of list by lambda key and adding the missing ascii letters in end as zeroes:

import string

text1 = 'acbbmnhctrgnmmxfnfbmqrhnchfwcqwtacvtfhmecttvfcnvphch\
mpgdmebjdcqwhdfnfrcawhdfrkanahtcjaqxahpkmqdardfcnhcqwjmprdcttv\
fpkdftwaqbmnfhdcqhdarcrhdfzmnwrzfnfrkmsfqhdfjkcrrfwhdnmpxdhdfzc\
ttcqwrhmmwpkmqcqmkfqgmpqhnjnmcwzahdeaftwrmqfahdfndcqwhdfgahjdcw\
fqhanftjlcqardfwqmhclfrhaxfmeahzcrhmvfrffqhdfwcnsqfrrcqwhdfbarh\
dcwlcqardfwzahdahemnahzcrcgtfcngmtwzaqhfnwcjzahdrqmzpkmqhdfxnmp\
qwxmmwdfclfqrcawrgnmmxfgtcrkaqxdar'

letterCount = {}
for char in text1:
    letterCount[char] = letterCount.get(char, 0) + 1
    
sortedCharTuples = sorted(letterCount.items(),
                          key=lambda x: x[1], # sort by second value
                          reverse=True) # descending


print "Character count in descending frequency:"

for charTuple in sortedCharTuples:   
    print "%r = %d" % (charTuple[0], charTuple[1]) 
for letter in string.ascii_lowercase:
    if letter not in letterCount:
        print "%r = 0" % letter

Edited 5 Years Ago by pyTony: n/a

Comments
thanks

tonyjv thanks alot for your codes, it works and help me alot.
and sorry for my codes, i not familiar to use that, i will try to fix it next time.

another question is, i want to plot a histogram to show all the letters , but this time is from A to Z, also wanna show the letters if it is "0", (let x axis be the 26 letters and y be the number of the letters)

i tried to use the charTuple command, but im not familiar to use it , and is it possible you give me some helps thanks.


thanks.

i'm just wondering, if plot the hitogram, do i need to put the number for A-Z into array first?
and from the above codes, how could i do that?
thanks

this is what i did, please have a look
seems working
i have a question, just wanna change the xlabel to the 26 letters A B C D .... instead the values, anyone can help me with that?
thanks alot.

import string 

text1 = 'acbbmnhctrgnmmxfnfbmqrhnchfwcqwtacvtfhmecttvfcnvphchmpgdmebjdcqwhdfnfrcawhdfrkanahtcjaqxahpkmqdardfcnhcqwjmprdcttvfpkdftwaqbmnfhdcqhdarcrhdfzmnwrzfnfrkmsfqhdfjkcrrfwhdnmpxdhdfzcttcqwrhmmwpkmqcqmkfqgmpqhnjnmcwzahdeaftwrmqfahdfndcqwhdfgahjdcwfqhanftjlcqardfwqmhclfrhaxfmeahzcrhmvfrffqhdfwcnsqfrrcqwhdfbarhdcwlcqardfwzahdahemnahzcrcgtfcngmtwzaqhfnwcjzahdrqmzpkmqhdfxnmpqwxmmwdfclfqrcawrgnmmxfgtcrkaqxdar'

letterCount = {}
for char in text1:
    letterCount[char] = letterCount.get(char, 0) + 1
 
sortedCharTuples = sorted(letterCount.items(),
                          key=lambda x: x[1], # sort by second value
                          reverse=True) # descending

 
print ""
print "Character count in descending frequency:"
print ""
print "No. of Letters, Relative Frequencies"
for charTuple in sortedCharTuples:
   
    print "%r = %d" % (charTuple[0], charTuple[1]), '     ',((charTuple[1])/(len(text1)))*100,"%"
for letter in string.ascii_lowercase:
    if letter not in letterCount:
        letterCount[letter] = 0
        print "%r = 0" % letter, '     ',(0/(len(text1)))*100,"%"

sortedForPlot = sorted(letterCount.items(),
                          key=lambda x: x[0], # sort by first value
                          reverse=False) # A-Z

values = arange(26)
heights = zeros(26)
i=0
for charTuple in sortedForPlot:
    count = charTuple[1]
    #values[i] = charTuple[0]
    heights[i] = count
    i = i + 1
#print values
#print heights


figure ()
bar (values, heights)
grid (True)
title ('Graph')
xlabel ('.....???????????? what should i put here?????')
ylabel ('counts')

show()

Edited 5 Years Ago by cool0329: n/a

sorry guys, i think just added 2line to the previouse one, that one is not working
just need help for the graph, want to change the xlabel to the 26 letters A B C D .... instead of the number values, anyone can help me with that?

and please have a look my output and the groph, if need a change please tell me.
thanks alot.

from __future__ import division
from pylab import *


import string 

text1 = 'acbbmnhctrgnmmxfnfbmqrhnchfwcqwtacvtfhmecttvfcnvphchmpgdmebjdcqwhdfnfrcawhdfrkanahtcjaqxahpkmqdardfcnhcqwjmprdcttvfpkdftwaqbmnfhdcqhdarcrhdfzmnwrzfnfrkmsfqhdfjkcrrfwhdnmpxdhdfzcttcqwrhmmwpkmqcqmkfqgmpqhnjnmcwzahdeaftwrmqfahdfndcqwhdfgahjdcwfqhanftjlcqardfwqmhclfrhaxfmeahzcrhmvfrffqhdfwcnsqfrrcqwhdfbarhdcwlcqardfwzahdahemnahzcrcgtfcngmtwzaqhfnwcjzahdrqmzpkmqhdfxnmpqwxmmwdfclfqrcawrgnmmxfgtcrkaqxdar'

letterCount = {}
for char in text1:
    letterCount[char] = letterCount.get(char, 0) + 1
 
sortedCharTuples = sorted(letterCount.items(),
                          key=lambda x: x[1], # sort by second value
                          reverse=True) # descending

 
print ""
print "Character count in descending frequency:"
print ""
print "No. of Letters, Relative Frequencies"
for charTuple in sortedCharTuples:
   
    print "%r = %d" % (charTuple[0], charTuple[1]), '     ',((charTuple[1])/(len(text1)))*100,"%"
for letter in string.ascii_lowercase:
    if letter not in letterCount:
        letterCount[letter] = 0
        print "%r = 0" % letter, '     ',(0/(len(text1)))*100,"%"

sortedForPlot = sorted(letterCount.items(),
                          key=lambda x: x[0], # sort by first value
                          reverse=False) # A-Z

values = arange(26)
heights = zeros(26)
i=0
for charTuple in sortedForPlot:
    count = charTuple[1]
    #values[i] = charTuple[0]
    heights[i] = count
    i = i + 1
#print values
#print heights


figure ()
bar (values, heights)
grid (True)
title ('Graph')
xlabel ('.....???????????? what should i put here?????'or some whereelse to change the number values to the 26 letters ? thanks )
ylabel ('counts')

show()

I don't have pylab installed, but looking at page 15 of http://matplotlib.sourceforge.net/Matplotlib.pdf, it looks like you -might- be able to do something crazy like:

xlabel('letters')
ylabel('counts')
axis('a', 'z', 0, max_count)

If that doesn't work, I'd see if there are any transformation functions you can apply, that will take whatever values it wants to put along the x-axis, and lookup what to replace them with.

Other than that, consider starting with a more general-purpose drawing API. TkInter is quite primitive, wxWidgets seems popular on this forum, and I'm working with PyQt -- which I absolutely love -- for general-purpose user-interface development (but it can be painful to build and install).

This article has been dead for over six months. Start a new discussion instead.