Indexing list of lists

Hilde Roth hildegarde_roth at yahoo.de
Thu Sep 18 20:21:43 CEST 2003


> It will be harder to parse.

No because in many cases the reason why you want to use the syntax
l[seq] is that you already have seq, so you would refer to it by name
and "l[s]" is definitely not hard to parse.

>     l[:3] would be l[(0, 3)]
>     l[3:] would be l[(3,-1)]

Not at all. I was suggesting to use a semi-colon not a colon. Thus if 
l is (10,20,30,40), l[:3] -> (10,20,30) whereas l[(0,3)] -> (10, 30), 
i.e., same as in your class-based example, minus the parentheses, which
I now realize are superfluous (odd that python allows you to omit the
parentheses but not the commas).

> 2. Slicing two dimensional object will not be possible as the notion
>    you proposed is used just for that (ex. l[1,2] which is equivalent
>    to l[(1,2)] see below), and Numeric and numarray use it.

Same misunderstanding as above, I believe. Let, e.g., l be 
((1,10,100),(2,20,200),(3,30,300),(4,40,400)). Then l[2:; 1:] ->
[(30, 300), (40, 400)]. This is equivalent to [i[1:] for i in l[2:]]
but, at least to me, it is the index notation that is easier to parse.

Incidentally, it strikes me that there are a lot of superfluous
commas. Why not just (1 10 100) or even 1 10 100 instead of (1,10,100)? 
The commas do make the expression harder to parse visually.

> >>> # multi dimensional indexing
> >>> c[1,3]

I disagree that this is "multidimensional". You are passing a list 
and getting back a list, so this is still flat. I think you are
confusing dimensionality and cardinality.

> Numeric and numarray use it

This is good if it is true (I have yet to look at these two because
my work is not primarily numerical) but, again, the restriction of this
syntax to arrays, and arrays of numeric values at that, strikes me
as completely arbitrary.

The bottom line is that python claims to be simple but has a syntax
which, in this little corner at least, is neither simple nor regular:
xranges and slices are both sequence abstractions but are used in
different contexts and have different syntaxes; C-style arrays are
treated differently than lists of lists of ..., although they are
conceptually equivalent; numeric structures are treated differently
than non-numeric ones etc etc

Oh well. Maybe a future PEP will streamline all that. 

Hilde




More information about the Python-list mailing list