Slice a list of lists?

Jonno jonnojohnson at gmail.com
Thu Sep 9 18:33:45 CEST 2010


On Thu, Sep 9, 2010 at 10:58 AM, Robert Kern <robert.kern at gmail.com> wrote:
> Please keep responses on the mailing list. However, I will reply below
> this one time.
>
> On Thu, Sep 9, 2010 at 10:35, Jonno <jonnojohnson at gmail.com> wrote:
>> On Wed, Sep 8, 2010 at 4:26 PM, Robert Kern <robert.kern at gmail.com> wrote:
>>> A motivating example:
>>>
>>> [~]
>>> |1> import numpy
>>>
>>> [~]
>>> |2> a = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
>>>
>>> [~]
>>> |3> a
>>>
>>> array([[1, 2, 3],
>>>       [4, 5, 6],
>>>       [7, 8, 9]])
>>>
>>> [~]
>>> |4> a[0,2]
>>> 3
>>>
>>> [~]
>>> |5> a[2,0]
>>> 7
>>>
>>> [~]
>>> |6> a[0,:]
>>> array([1, 2, 3])
>>>
>>> [~]
>>> |7> a[:,0]
>>> array([1, 4, 7])
>>>
>>>
>>> --
>>> Robert Kern
>>
>> Thanks Robert. I'm actually much more familiar and comfortable with
>> arrays so I know how to slice them. The problem is that the data comes
>> from lines in a file which are not all of equal length.
>> My understanding is that I need to manipulate the data as lists until
>> I can get all the lists of equal length at which point I can make an
>> array from it.
>
> If you have ragged arrays, then you do not want to slice down columns
> to begin with. You might run into a row that does not have a value at
> that column. If you need to slice down columns (whether the object
> remains a list of lists or a numpy array), then you need to create a
> rectangular array regardless. Do that first, then I recommend using
> numpy arrays. Slicing down columns (probably) won't help you with
> that.

Understood. My data actually has 2 features which make it
non-rectangular in most circumstances. One is the extra element at the
start of every nth line (I now know how to remove that) and the second
is that some lines contain more data than others. My data is
essentially a m,m,n array but data from each row is shifted to the
next line when the line is full (4 columns). A new row then starts a
new line.

I want to pick out the data and assign it to an array but I'm
wondering whether I should first try to take the data on the extra
lines, remove it and stick it on the end of the original line (the
data from one "row" could extend onto multiple lines). Or whether I'm
better trying to create the array using loops to index my way through
the lines the way they already are laid out. Hope this makes sense.



More information about the Python-list mailing list