# Split a list every n times and always skip one row

another question:

I have a very long Excel data sheet of z-values. I converted the Excel sheet into CSV. to make it less heavy on calculation time.

The data is divided into packs of $197\times197$. After every pack there is a row with only zeros to divide the packs. For every block, I would like to plot a contour plot. Therefore I need to extract the individual blocks.

Something like: Split at every 197*197 Element, but skip 1*197 Element after each split (row for separation) How can I do that?

Thanks for any help!

• Why not Import[], delete the zero rows, and use Partition[]? Commented Aug 2, 2016 at 16:55
• How do you delete the zero rows ? Commented Aug 2, 2016 at 17:00
• Have you seen DeleteCases[]? Commented Aug 2, 2016 at 17:02
• Thanks. But the data is given as one long list {{-----}} so I do not want to delete all zeors. Commented Aug 2, 2016 at 17:08
• You can use DeleteCases[] to delete an entire row; however, if your data has a legitimate row of zeros as opposed to just being a separator, then you should have chosen a better separator. Commented Aug 2, 2016 at 17:10

You could use SequenceCases, which lets you specify a replacement rule for each sequence it finds, so the delimiting zeros can be dropped that way. I'll construct a testList with a smaller number of rows and columns, then apply the splitting:

nRow = 3;
nCol = 5;
block = RandomReal[10,{nRow,nCol}];
testList = Flatten[Riffle[block, Table[{zeros}, nRow]]]

(*
==> {4.41795, 1.96463, 2.73945, 9.01363, 8.49372, 0, 0, 0, 0,
0, 1.14841, 7.57119, 1.01441, 7.9697, 4.21982, 0, 0, 0, 0, 0,
7.75255, 0.431971, 2.10869, 8.60605, 5.91062, 0, 0, 0, 0, 0}
*)

SequenceCases[testList,
{x : Except[Repeated[0, {nRow}]] .., Repeated[0, {nRow}]} :> {x}]

(*
==> {{4.41795, 1.96463, 2.73945, 9.01363, 8.49372},
{1.14841, 7.57119, 1.01441, 7.9697, 4.21982},
{7.75255, 0.431971, 2.10869, 8.60605, 5.91062}}
*)


In the search pattern for SequenceCases, the zeros are assumed to occur at the end of a sequence in which the preceding elements are not sequences of nRow zeros. These elements are labeled x and collected in {x} using the rule :>.

Using Jens' data

list =
{4.41795, 1.96463, 2.73945, 9.01363, 8.49372, 0, 0, 0, 0, 0,
1.14841, 7.57119, 1.01441, 7.9697, 4.21982, 0, 0, 0, 0, 0,
7.75255, 0.431971, 2.10869, 8.60605, 5.91062, 0, 0, 0, 0, 0};


SequenceSplit (new in 11.3) seems to be custom-tailored for this task

SequenceSplit[list, {0}]


{{4.41795, 1.96463, 2.73945, 9.01363, 8.49372}, {1.14841, 7.57119, 1.01441, 7.9697, 4.21982}, {7.75255, 0.431971, 2.10869, 8.60605, 5.91062}}

With SequenceCases we can get the same result:

SequenceCases[list, {Except[0] ..}]


{{4.41795, 1.96463, 2.73945, 9.01363, 8.49372}, {1.14841, 7.57119, 1.01441, 7.9697, 4.21982}, {7.75255, 0.431971, 2.10869, 8.60605, 5.91062}}

Grabbing Jens' data and using Split:

list =
{4.41795, 1.96463, 2.73945, 9.01363, 8.49372, 0, 0, 0, 0, 0,
1.14841, 7.57119, 1.01441, 7.9697, 4.21982, 0, 0, 0, 0, 0,
7.75255, 0.431971, 2.10869, 8.60605, 5.91062, 0, 0, 0, 0, 0};

Most /@ Cases[{_, __}]@Split[list, # =!= 0 &]

(*{{4.41795, 1.96463, 2.73945, 9.01363, 8.49372},
{1.14841, 7.57119, 1.01441, 7.9697, 4.21982},
{7.75255, 0.431971, 2.10869, 8.60605, 5.91062}}*)