Indexing list of lists
Lukasz Pankowski
lupan at zamek.gda.pl
Sat Sep 20 12:18:15 CEST 2003
hildegarde_roth at yahoo.de (Hilde Roth) writes:
> 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.
So you want l[seq] to be a shorter way for current
[l[i] for i in seq]
Which is pythonic as it is explicit, easier to read if the code is not
written by you yesterday, although in some situations your interpretation
of l[seq] might be guessable from the context.
> 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).
Sorry for my misunderstanding, yes it would be nice to have a
posibility to index a sequence with a list of indices, here is a pure
Python (>= 2.2) implementation of the idea:
class List(list):
def __getitem__(self, index):
if isinstance(index, (tuple, list)):
return [list.__getitem__(self, i) for i in index]
else:
return list.__getitem__(self, index)
>>> l = List(range(0, 100, 10))
>>> l[0,2,3]
[0, 20, 30]
but in this simple using both commas and slices will not work as
expected
>>> l[0,1,7:]
[0, 10, [70, 80, 90]]
>
>> 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.
>
This will will work until there are no expressions in the sequence.
If there are it is harder to read (and may be more error prone)
(1 + 3 4 + 2)
>> >>> # 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.
>
That is the notion of multidimensional indexing in Python.
>> 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.
Here is an example of two dimentional Numeric array and it's indexing:
>>> from Numeric import *
>>> reshape(arange(9), (3,3))
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> a = reshape(arange(9), (3,3))
>>> a[1][0]
3
>>> a[1,0]
3
>>> a[(1,0)]
3
So currently indexing with sequence has a settled meaning of
multidimensional indexing if lists and tuples would allow indexing by
sequence, than either:
1. it might be confused with multidimensional indexing of numeric
types (the same notion for two different things)
2. it will require rework of multidimensional indexing maybe with your
semicolon notion, but will introduce incompatibilities (back and
forward)
>
> 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
>
With multidimensional arrays and list of lists you may both write
a[i][j], so it is consistent, with arrays you may write a[i,j] if you
know that you have an 2-dim array in your hand.
--
=*= Lukasz Pankowski =*=
More information about the Python-list
mailing list