Using namedtuples field names for column indices in a list of lists
Deborah Swanson
python at deborahswanson.net
Mon Jan 9 16:52:01 EST 2017
breamoreboy at gmail.com wrote, on January 09, 2017 8:32 AM
>
> On Monday, January 9, 2017 at 2:22:19 PM UTC, Tim Chase wrote:
> > On 2017-01-08 22:58, Deborah Swanson wrote:
> > > 1) I have a section that loops through the sorted data, compares
two
> > > adjacent rows at a time, and marks one of them for deletion if the
> > > rows are identical.
> > > and my question is whether there's a way to work with two adjacent
> > > rows without using subscripts?
> >
> > I usually wrap the iterable in something like
> >
> > def pairwise(it):
> > prev = next(it)
> > for thing in it:
> > yield prev, thing
> > prev = thing
> >
> > for prev, cur in pairwise(records):
> > compare(prev, cur)
> >
> > which I find makes it more readable.
> >
> > -tkc
>
> Or from
> https://docs.python.org/3/library/itertools.ht> ml#itertools-recipes:-
>
> def pairwise(iterable):
> "s -> (s0,s1), (s1,s2), (s2, s3), ..."
> a, b = tee(iterable)
> next(b, None)
> return zip(a, b)
>
> This and many other recipes are available in the
> more-itertools module which is on pypi.
Thanks, I'll keep this since I seem to do pairwise comparisons a lot.
I'm going to try using set or OrderedDict for the current problem, per
Peter's suggestion, but if I can't make that work, this surely will.
More information about the Python-list
mailing list