For example I have a non-ordered list of values [10, 20, 50, 200, 100, 300, 250, 150]
I have this code which returns the next greater value:
def GetN
Edit: Ah, I used templist
instead of list
-- hence the confusion. I didn't mean it to be a one-line function; you still have to do the conversions. (Of course, as Mike DeSimone rightly points out, using list as a variable name is a terrible idea!! So I had a good reason for being confusing. :)
To be more explicit about it, here's a slightly streamlined version of the function (fixed to test properly for an empty list):
def GetNextHighTemp(self, temp, templist):
templist = (int(t) for t in templist if t != '')
templist = [t for t in templist if t < int(temp)]
if templist: return max(templist)
else: return None # or raise an error
Thanks to Mike for the suggestion to return None
in case of an empty list -- I like that.
You could shorten this even more like so:
def GetNextHighTemp(self, temp, templist):
try: return str(max(int(t) for t in templist if t != '' and int(t) < int(temp)))
except ValueError: return None # or raise a different error