min max of a list

Steven Bethard steven.bethard at gmail.com
Fri May 6 18:03:56 EDT 2005


Peter Hansen wrote:
> querypk at gmail.com wrote:
> 
>> I get a syntax error in  :
>>
>>  py> [(min((abs(p - v), v) for v in valleys + [0] if v < p)[1],
>> ...   p,
>> ...   min((abs(p - v), v) for v in valleys if v > p)[1])
>> ...  for p in peaks]
> 
> 
> I think we already covered the part where you were using an older 
> version of Python.  In this case, the missing feature is "generator 
> expressions" and they are inside the two min() calls.
> 
> You might want to consider upgrading...

But if you can't, you should write this as something like:

[(min([(abs(p - v), v) for v in valleys + [0] if v < p])[1],
   p,
   min([(abs(p - v), v) for v in valleys if v > p])[1])
  for p in peaks]

Note the extra brackets in the min calls.

STeVe



More information about the Python-list mailing list