Getting the minimum from a list.
Seo Sanghyeon
unendliche at hanmail.net
Sun May 25 23:51:41 EDT 2003
anson wrote:
> (syntax error code)
I assume it was:
----
data = [
[("file", 101, "function f"), 456, 80000],
[("file2", 102, "function"), 321, 90000]
]
----
This works:
----
def decorate(x):
return x[1], x
def undecorate(x):
return x[1]
>>> undecorate(min(map(decorate, data)))
[('file2', 102, 'function'), 321, 90000]
>>> data = map(decorate, data)
>>> data.sort()
>>> data = map(undecorate, data)
>>> data
[[('file2', 102, 'function'), 321, 90000], [('file', 101, 'function
f'), 456, 80000]]
----
Basic idea is, let decorate() to return tuple of keys that you want to
use for sorting and original record, and undecorate() to get rid of
keys. You can make decorate() as complex as you want.
Ah, and this uses the fact that tuples in Python are compared in
order.
-- Seo Sanghyeon
More information about the Python-list
mailing list