"Natural" use of cmp= in sort

Paddy paddy3118 at gmail.com
Tue Nov 11 03:03:07 CET 2014


On Monday, 10 November 2014 19:44:39 UTC, Ian  wrote:
> On Mon, Nov 10, 2014 at 12:19 PM, Peter Otten <xxx at yyy> 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').

Thanks. I will look into this...



More information about the Python-list mailing list