where or filter on list

charles.hebert at gmail.com charles.hebert at gmail.com
Wed Aug 30 09:22:34 EDT 2006


Thanks all !

> Question: what if two values are equidistant?

>>> def closest(foo,v):
...     intermed = [(abs(v), v) for v in foo]
...     intermed.sort()
...     return [x[1] for x in intermed if x[0] == intermed[0][0]]
...
>>> print closest([-20,-10,10,15],0)
[-10, 10]

Works fine !

(now with sorted ... ?)




More information about the Python-list mailing list