"Natural" use of cmp= in sort

Ian Kelly ian.g.kelly at gmail.com
Mon Nov 10 14:43:39 EST 2014


On Mon, Nov 10, 2014 at 12:19 PM, Peter Otten <__peter__ at web.de> wrote:
> I'm not sure this works. I tried:

Here's a simpler failure case.

>>> ineq = """f2 > f3
... f3 > f1"""

[Previously posted code elided]

>>> greater_thans
set([('f3', 'f1'), ('f2', 'f3')])
>>> sorted(all_f, cmp=lambda t1, t2: 0 if t1==t2 else
...             (1 if (t1, t2) not in greater_thans else -1))
['f1', 'f2', 'f3']

Note that the greater_thans set is missing the implication by
transitivity that f2 > f1, so the given cmp function would
inconsistently return -1 for both comparisons cmp('f1', 'f2') and
cmp('f2', 'f1').



More information about the Python-list mailing list