Questiion on list

Quinn Dunkan quinn at upchuck.ugcs.caltech.edu
Thu Jun 27 01:11:06 EDT 2002


On Thu, 27 Jun 2002 14:36:03 +0000, SiverFish <occeanlinux at linuxmail.org> wrote:
>I got the list of words now i want to calculate how many word in that
>list which is the same word (insensitive case),can anyone show me how to do
>it for example ls = ['abc','cd','abc','adf',abc','dwc','cd']
>it will show abc = 3 ,cd = 2, adf = 1, dwc=1

import string
d = {}
for e in map(string.lower, ls):
    d[e] = d.get(e, 0) + 1
freqs = [ (count, w) for w, count in d.items() ]
freqs.sort()
freqs.reverse()
print ', '.join([ '%s = %d' %(count, w) for count, w in freqs ])



More information about the Python-list mailing list