Reputation: 465
I have a list of tuples containing 2 numbers like below. I essentially need to know the current minimum for how far into the list I am, hopefully outputting something like l2.
l = [(30, -182), (55, -160), (72, -143), (92, -183)]
l2 = [(30, -182), (55, -182), (72, -182), (92, -183)]
I've tried using enumerate but keep encountering:
ValueError: min() arg is an empty sequence
The code I'm using so far:
for i,(ss,en) in enumerate(sdat):
print ss, min(x[1] for x in sdat[:i])
Any help is greatly appreciated!
Upvotes: 0
Views: 39
Reputation: 60127
Well, you're getting a bit confused.
for i,(ss,en) in enumerate(sdat):
You don't need indexes as long as you can keep history, so there's your first hint.
print ss, min(x[1] for x in sdat[:i])
You're rerunning min
(incorrectly) every time; this is a lot of work to compare two numbers.
Just do this manually:
sdat = [(30, -182), (55, -160), (72, -143), (92, -183)]
if sdat:
ss, en = sdat[0]
for ss, new_en in sdat:
if new_en < en:
en = new_en
print((ss, en))
#>>> (30, -182)
#>>> (55, -182)
#>>> (72, -182)
#>>> (92, -183)
Also, to prove that I'm supeh functional and stuff, here's what I'd actually do:
from itertools import accumulate
def oddmin(a, b):
return b[0], min(a[1], b[1])
accumulate(l, oddmin)
Requires 3.3 and up, so this won't work for Jsg91.
Upvotes: 3