Using namedtuples field names for column indices in a list of lists

Antoon Pardon antoon.pardon at rece.vub.ac.be
Mon Jan 9 05:35:18 EST 2017


Op 09-01-17 om 07:58 schreef Deborah Swanson:
> Peter Otten wrote, on January 08, 2017 5:21 AM
>> Deborah Swanson wrote:
>>
>>> Peter Otten wrote, on January 08, 2017 3:01 AM
>>  
>> Personally I would recommend against mixing data (an actual location)
> and 
>> metadata (the column name,"Location"), but if you wish my code can be 
>> adapted as follows:
>>
>> infile = open("dictreader_demo.csv")
>> rows = csv.reader(infile)
>> fieldnames = next(rows)
>> Record = namedtuple("Record", fieldnames)
>> records = [Record._make(fieldnames)]
>> records.extend(Record._make(row) for row in rows)
> Works like a charm. I stumbled a bit changing all my subscripted
> variables to namedtuples and rewriting the inevitable places my code
> that didn't work the same. But actually it was fun, especially deleting
> all the sections and variables I no longer needed. And it executes
> correctly now too - with recognizable fieldnames instead of my quirky
> 2-letter code subscripts.  All in all a huge win!
>
> I do have two more questions.
>
> 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.
>
> I'm using 
>
> for i in range(len(records)-1):
>     r1 = records[i]
>     r2 = records[i+1]
>     if r1.xx = r2.xx:
> 		.
> 		.
> and my question is whether there's a way to work with two adjacent rows
> without using subscripts?  
>
> Even better, to get hold of all the records with the same Description as
> the current row, compare them all, mark all but the different ones for
> deletion, and then resume processing the records after the last one?

If I understand correctly you want something like:

    records.sort(key = lamda rec: rec.xx)

AKA 

    from operator import attrgetter
    records.sort(key = attrgetter('xx'))

or maybe:

    records.sort(key = lambda rec: (rec.xx,) + tuple(rec))

-- 
Antoon Pardon



More information about the Python-list mailing list