Namedtuples problem
Deborah Swanson
python at deborahswanson.net
Thu Feb 23 20:38:32 EST 2017
Ben Bacarisse wrote, on February 23, 2017 4:24 PM
>
> "Deborah Swanson" <python at deborahswanson.net> writes:
>
> >> -----Original Message-----
> >> From: Erik [mailto:python at lucidity.plus.com]
> >> Sent: Thursday, February 23, 2017 2:09 AM
> >> To: python at deborahswanson.net; python-list at python.org
> >> Subject: Re: Namedtuples problem
> >>
> >>
> >> Hi,
> >>
> >> On 23/02/17 09:38, Deborah Swanson wrote:
> >> > group[[idx][records_idx[label]]]
> >> > gets an IndexError: list index out of range
> >>
> >> [snip]
> >>
> >> > Can anyone see why I'm getting this Index error? and how
> to fix it?
> >>
> >> 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]
>
> Sure. No trouble there.
>
> > gets the right answer, but
> >
> > group[[idx][records_idx[label]]],
> > where idx = 0 and records_idx[label]] = 4
> >
> > gets the IndexError.
>
> group is not involved in the index error. Just write
>
> [idx][records_idx[label]]
>
> and you'll get the error. Wrapping that up in group[...]
> won't make a difference. In fact, since idx is 0 and
> records_idx[label]] is 4, you get the same error (for the
> same reason) by typing
>
> [0][4]
>
> into a Python REPL. Does it still seem weird?
No, and you also put your finger on (psrt of) the real problem. See the
reply I just sent to MRAB.
> This is just another shot at explaining the issue because,
> sometimes, different words can help. I thought the message
> you are replying to (as well as others) explained it well,
> but it obviously it did not hit the spot.
>
> <snip>
> --
> Ben
No, the spot wasn't getting hit, but it has been now. Thanks for
replying.
More information about the Python-list
mailing list