[EMAIL PROTECTED] writes: > 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!!
I won't comment on the algorithm, but I think you should try to find better names for your variables. In the snippet above you have x, res, dic, newd, length, getvals which don't give much of a clue as to what they are used for. e.g. * dic = {} We know it's a dict, but a dict of what? * newd = dic.items() Sounds like 'new dictionary', but obviously isn'tas it is a list of key,value pairs. * length = len(getvals) Again, we know it's a length, but the length of what? HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list