Namedtuples problem
Deborah Swanson
python at deborahswanson.net
Thu Feb 23 21:24:05 EST 2017
Erik wrote, on February 23, 2017 4:03 PM
>
> On 23/02/17 23:00, Deborah Swanson wrote:
> >> It looks to me like you are indexing into a single-element
> list that
> >> you are creating using the literal list syntax in the middle of
> >> the expression.
> >
> > Actually, group is essentially a 2-element list. Each group
> has a list
> > of rows, and each row has a set of fields. group has to be
> indexed by
> > row index and field index. (This is a namedtuple configuration.)
> >
> > The weirdness is that
> >
> > group[0][4]
> >
> > gets the right answer, but
> >
> > group[[idx][records_idx[label]]],
> > where idx = 0 and records_idx[label]] = 4
> >
> > gets the IndexError.
>
> So remove the outermost square brackets then so the two expressions
are
> the same (what I - and also Steven - mentioned is correct: you are
> creating a single-element list and indexing it (and then using the
> result of that, should it work, to index 'group')).
I see that clearly now, I just couldn't understand it from what you and
Steven were saying.
> The same thing as "group[0][4]" in your example is:
>
> group[idx][records_idx[label]]
>
> (assuming you have all those things correct - I haven't studied your
> code in a lot of detail).
You have it exactly correct, so no code study required. ;)
> Your new example expressed using the original construct you posted is:
>
> group[[0][4]]
>
> ... see the extra enclosing square brackets?
>
> E.
Yes, I see them now, and deleting them makes everything work like it
should.
More information about the Python-list
mailing list