get the sum of differences between integers in a list
Peter Otten
__peter__ at web.de
Wed Sep 21 11:12:04 EDT 2016
Peter Otten wrote:
> This is not as efficient as it should be -- the gaps are calculated twice,
> the check for the first and the last position is repeated on every
> iteration lots of packing and unpacking... --
To address some of my own criticism:
def lonely(triple):
left, value, right = triple
return left and right
def grouped(values):
a, b, mid = tee(values, 3)
gaps = (y - x != 1 for x, y in zip(a, islice(b, 1, None)))
left, right = tee(gaps)
triples = zip(
chain([True], left),
mid,
chain(right, [True])
)
for key, group in groupby(triples, lonely):
yield key, (value for left, value, right in group)
> either groupby() is the
> wrong tool or I'm holding it wrong ;)
Most certainly still true.
More information about the Python-list
mailing list