letter frequency counter / your thoughts..

umpsumps at gmail.com umpsumps at gmail.com
Wed May 7 12:39:50 EDT 2008


Hello,

Here is my code for a letter frequency counter.  It seems bloated to
me and any suggestions of what would be a better way (keep in my mind
I'm a beginner) would be greatly appreciated..

def valsort(x):
	res = []
	for key, value in x.items():
		res.append((value, key))
	return res

def mostfreq(strng):
	dic = {}
	for letter in strng:
		if letter not in dic:
			dic.setdefault(letter, 1)
		else:
			dic[letter] += 1
	newd = dic.items()
	getvals = valsort(newd)
	getvals.sort()
	length = len(getvals)
	return getvals[length - 3 : length]

thanks much!!



More information about the Python-list mailing list