Welcome, guest | Sign In | My Account | Store | Cart

Comparing over multiples keys is a common task in Python. To make it easier, multi_cmp creates a compound comparison function using the supplied keys and getter.

Python, 15 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def multi_cmp(keys, getter):                                                    
    """                                                                         
    keys - a list of keys which the getter will used compare with
    getter - a getter, usually itemgetter or attrgetter from operator
                
    This function builds a compound compare over multiple keys using the        
    supplied getter.           
    """             
    def _cmp_with_keys(x,y):
        for key in keys: 
            retVal = cmp(getter(key)(x), getter(key)(y))                        
            if retVal:                                                          
                return retVal
        return 0
    return _cmp_with_keys                                                       
    

The function allows you to turn something like: sorted(my_list, cmp=lambda x,y: cmp(x.attr1, y.attr1) or cmp(x.attr2, y.attr2)) into: sorted(my_list, cmp=multi_cmp(["attr1", "attr2"], operator.attrgetter))

1 comment

Steven Bethard 16 years, 7 months ago  # | flag

use key= and multiple argument form of attrgetter. This is already supported using the key= argument to sorted() and the multiple argument form of attrgetter:

>>> class Foo(object):
...     def __init__(self, bar, baz):
...         self.bar = bar
...         self.baz = baz
...     def __repr__(self):
...         return 'Foo(%r, %r)' % (self.bar, self.baz)
...
>>> items = [Foo(2, 'a'), Foo(1, 'b'), Foo(2, 'b')]
>>> sorted(items)
[Foo(1, 'b'), Foo(2, 'a'), Foo(2, 'b')]
>>> sorted(items, key=operator.attrgetter('baz', 'bar'))
[Foo(2, 'a'), Foo(1, 'b'), Foo(2, 'b')]
Created by Richard Harris on Mon, 10 Sep 2007 (PSF)
Python recipes (4591)
Richard Harris's recipes (3)

Required Modules

  • (none specified)

Other Information and Tasks