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.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
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
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
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).
raptr_dflo
Practically a Master Poster
602 posts since Aug 2010
Reputation Points: 76
Solved Threads: 82