lambda to Sort Dictionary.items() by Value

Jim Dennis jimd at vega.starshine.org
Sun Feb 17 15:36:02 EST 2002


 I'm playing with text and I decided to write a simple histogram
 function (calculating letter occurence frequencies, in this case).

 In the course of that I found two simple uses for lambda one of 
 which seems like it would be a very common idiom in Python.

 Here's the function:

def histogram(s):
    """ Calculate the histogram for letters in a sequence
        returns histogram dictionary and a sequence sorted by  
        decreasing frequency (rarest last) (case insensitive)"""
    letters = string.letters
    h = {}
    for i in s:
        if i in letters:
            i = i.lower()
            if h.has_key(i): h[i] += 1
            else: h[i] = 1
    i = h.items()
    i.sort(lambda (k1,v1),(k2,v2): cmp(v2,v1) ) # sort by value, not by key
    i=string.join(map(lambda (x,y):x, i),"")  # extract letters from each
    return (h,i)

 The useful idiom seems to be in the comparison function for the sort().  
 When sorting the items of a dictionary by value (rather than by key) it 
 seems like using the lambda (k1,v1),(k2,v2): v2 - v1 to decompose
 the two tuples and return a comparison is the most concise way to 
 express it.

 It seems like one might need to sort dictionary items by value
 pretty often.

 The other use of lambda here is less interesting; I'm just extracting
 items from tuples.

 I guess these are slightly more readable as:

def cmpvalr((a,b),(c,d)):
	"""Compare two tuples by their 2nd (value) elements: reverse"""
	return cmp(d,b)

 ...

items = h.items()
items.sort(cmpval)

 Incidentally, in occurs to me that using cmp() allows this
 idiom to use any comparable types --- so it would would with
 any dictionary so long as the values were comparable.  
 Initially I'd written it as v2 - v1 --- which works for numbers
 but not for other types.
  



More information about the Python-list mailing list