Namedtuple problem #32.11.d
Deborah Swanson
python at deborahswanson.net
Tue Jun 6 00:49:52 EDT 2017
I have a list of namedtuples:
[{Record}(r0=v0, r1=v1,...,r10=v10,r11='',...r93='')
. . .
{Record}(r0=v0, r1=v1,...,r10=v10,r11='',...r93='')]
In the first section of code, I process some of the first 10 columns
(r0=v0, r1=v1,...,r10=v10), and place the results in blank columns, also
in the first 10 columns.
In the second section of code, I'd like to put calculated values in
columns 11-93 (r11='',...r63=''), in which 3 columns will be calculated
28 times for each Record.
Is there a way to do this as a step in the loop:
for idx, r in enumerate(records):
. . .
(ideally I'd like to have a loop here, or a function, that would
calculate the 28 triplets
and replace their empty namedtuples with their calculated
values,
but this is what I've failed to successfully do.)
I've tried writing a function, to either calculate and write 1) all 28
triplets, 2) one triplet, or 3) one column at a time, that would be
called in the body of the main loop.
The general form of this function has been:
def savedata(Records, row, column, data):
r = Records[row]
col = getattr(r, column)
r = r._replace(col = data)
but in every case I've tried this, it has failed.
Because, I can't say
r = r._replace(getattr(r, column) = data)
because that gets "can't assign to a function call".
And while col ( = getattr(r, column) ) holds the correct value before
r = r._replace(col = data)
all I get for r.column after the ._replace is an empty string, and I
have no idea how that happens.
I'm about to rewrite to process first 10 columns as namedtuples, and
then unroll the namedtuples into a list of lists, and process the 28
triplets in two for loops.
That will work, but I'm wondering if anyone can suggest a way to make my
function, or one that accomplishes the same task, so as to preserve the
namedtuples, and avoid the need for a second, dual nested loop to finish
the second part of the problem.
(Note: I have the same dilemma using recordclass instead of
namedtuples.)
Deborah
More information about the Python-list
mailing list