freq function

Peter Otten __peter__ at web.de
Sun Aug 22 14:29:31 CEST 2010


Dirk Nachbar wrote:

> Here is a function which takes any list and creates a freq table,
> which can be printed unsorted, sorted by cases or items. It's supposed
> to mirror the proc freq in SAS.
> 
> Dirk
> 
> def freq(seq,order='unsorted',prin=True):
>     #order can be unsorted, cases, items
> 
>     freq={}
>     for s in seq:
>         if s in freq:
>             freq[s]+=1
>         else:
>             freq[s]=1
>     if prin==True:
>         print 'Items=',len(seq),'Cases=',len(freq)
>         print '------------------------'
>         if order=='unsorted':
>             for k in freq.keys():
>                 print k,freq[k],float(freq[k])/len(seq)
>         elif order=='cases':
>             #http://blog.client9.com/2007/11/sorting-python-dict-by-
> value.html
>             freq2=sorted(freq.iteritems(), key=lambda (k,v):
> (v,k),reverse=True)

Sorting in two steps gives a slightly better result when there are items 
with equal keys. Compare

>>> freq = {"a": 2, "b": 1, "c": 1, "d": 2}
>>> sorted(freq.iteritems(), key=lambda (k, v): (v, k), reverse=True)
[('d', 2), ('a', 2), ('c', 1), ('b', 1)]

with

>>> freq2 = sorted(freq.iteritems(), key=lambda (k, v): k)
>>> freq2.sort(key=lambda (k, v): v, reverse=True)
>>> freq2
[('a', 2), ('d', 2), ('b', 1), ('c', 1)]

Here the keys within groups of equal frequency are in normal instead of 
reversed order.



More information about the Python-list mailing list