Namedtuples problem

Erik python at lucidity.plus.com
Thu Feb 23 19:02:53 EST 2017


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')).

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).

Your new example expressed using the original construct you posted is:

group[[0][4]]

... see the extra enclosing square brackets?

E.



More information about the Python-list mailing list