jbochi
jbochi

Reputation: 29644

Optimize function to sort a list of tuples

I need to sort a list of tuples by first item in descending order, and then by second item in ascending order.

To do this, I have implemented the following function, but I think it could be faster.

>>> compare = lambda a, b: -cmp(b[1], a[1]) if b[0] == a[0] else cmp(b[0], a[0])
>>> sorted([(0, 2), (0, 1), (1, 0), (1, 2)], cmp=compare)
[(1, 0), (1, 2), (0, 1), (0, 2)]

Is it possible to optimize it? See the comparison against the built-in function:

>>> timeit.Timer(stmt='sorted([(int(random.getrandbits(4)),int(random.getrandbits(4))) for x in xrange(10)], cmp=compare)', setup='import random; compare=compare = lambda a, b: -cmp(b[1], a[1]) if b[0] == a[0] else cmp(b[0], a[0])').timeit(100000)
4.0584850867917339
>>> timeit.Timer(stmt='sorted([(int(random.getrandbits(4)),int(random.getrandbits(4))) for x in xrange(10)])', setup='import random').timeit(100000)
2.6582965153393161

Upvotes: 1

Views: 564

Answers (2)

PaulMcG
PaulMcG

Reputation: 63749

How does this stack up for you?

compare = lambda a, b: cmp(b[0], a[0]) and cmp(a[1],b[1])

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838696

For me, it is a little faster to use a key instead of a comparison function, and arguably also easier to read:

sorted([(0, 2), (0, 1), (1, 0), (1, 2)], key = lambda x:(-x[0], x[1]))

This requires Python 2.4 or newer.

Upvotes: 8

Related Questions