letter frequency counter / your thoughts..

umpsumps at gmail.com umpsumps at gmail.com
Wed May 7 16:00:48 EDT 2008


That's a great suggestion Arnaud.  I'll keep that in mind next time I
post code. Thanks ;)


On May 7, 12:27 pm, Arnaud Delobelle <arno... at googlemail.com> wrote:
> umpsu... at gmail.com 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




More information about the Python-list mailing list