Indexing list of lists

H

Hilde Roth

So you want l[seq] to be a shorter way for current
[l for i in seq]

Which is pythonic as it is explicit, easier to read if the code is not
written by you yesterday


But consistency is pythonic, too ;-) and, as I pointed in another message,
if you accept l[sequence_abstraction], why not l[actual_sequence]??

Hilde
 
D

David C. Fox

Hilde said:
This is a red herring.

No, it's not. It is a hint that, despite the similarity of notation
between Matrix[j] and NestedList[j], there is something
fundamentally different between the two. See below for another example.
What do you expect a[;1] to return if a = [[], [1, 2, 3], [4], 5]?


Whatever error python returns if you ask, e.g., for (1 2 3)[4].

Hilde

Okay, here's a better example which I thought of just after I posted my
previous reply:

Given

x = [[1, 2], [3, 4]]

the statement

x[0] = [5, 6]

will result in a nested list x = [ [5, 6], [3, 4] ]. If you think of x
as a multi-dimensional array represented by a nested list, then I've
just replaced a row of the original array

1 2
3 4

with a new row, yielding

5 6
3 4

If we had an x[;0] notation, then you would expect to be able to do the
same thing to replace a column:

x[;0] = [7, 8]

Unfortunately, there is no pre-existing list representing the first
column of x, so x[;0] has to return a new list [1, 3], and assigning to
that new list has no affect on x.

Again, my point is that nested lists are a fundamentally different
structure than multi-dimensional arrays. For simple things like
x[j], you can use a nested list to represent a multi-dimensional
array, but if you actually want to manipulate a two-dimensional array
like a matrix, you are better off using a class designed for that
purpose, like the ones defined in Numeric Python.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,316
Latest member
naturesElixirCBDGummies

Latest Threads

Top