python - Groupby with function remembering state -
i processing many lines, want group them based on whether 1 value x in current line within 100 of value x in previous line.
for example
5, "hello" 10, "was" 60, "bla" 5000, "qwerty"
"hello", "was" , "bla" should 1 group, "qwerty" another.
is there way neatly solve groupby? solutions can think of feel bit hackish, taking dict default argument previous value in , updating each time function (key) in groupby called.
you write simple class encapsulate temp variables, use method of class key function:
class keyclass(object): def __init__(self): self.lastvalue = none self.currentkey = 1 def handleval(self, val): if self.lastvalue not none , abs(val - self.lastvalue) > 100: self.currentkey += 1 self.lastvalue = val return self.currentkey >>> [(k, list(g)) k, g in itertools.groupby(data, keyclass().handleval)] [(1, [1, 2, 100, 105]), (2, [300, 350, 375]), (3, [500]), (4, [800, 808])]
just fun, came rather mind-bending way using send
method of pre-advanced generator key function:
def keygen(): curkey = 1 newval = yield none while true: oldval, newval = newval, (yield curkey) if oldval none or abs(newval-oldval) > 100: curkey += 1 key = keygen() next(key) >>> [(k, list(g)) k, g in itertools.groupby(data, key.send)] [(1, [1, 2, 100, 105]), (2, [300, 350, 375]), (3, [500]), (4, [800, 808])]
wrapping head around may exercise in understanding .send
(it me!).
Comments
Post a Comment