Python list traversal with gaps
<p>Hi I have a multidimensional list such as:</p>

<pre><code>my_list = [[1,2,3,1,2],[1,0,3,1,2],[1,0,0,0,2],[1,0,3,0,2]]
</code></pre>

<p>where 0 represents a gap between two pieces of data. </p>

<p>What I need to do is iterate through the list and keep track of how many gaps are in each sublist and throw away the zeros. I think the best way is to break each sublist into chunks where there are zeros so I end up with smaller lists of integers and a number of gaps. Ideally, to form a new list which tells me the length of each chunk and number of gaps (i.e. chunks -1), such as:</p>

<pre><code>new_list = [[5, 0], [[1, 3], 1], [[1, 1], 1], [[1, 1, 1], 2]]
</code></pre>

<p>or probably better:</p>

<pre><code>new_list = [[5], [1, 3], [1, 1], [1, 1, 1]]
</code></pre>

<p>and I will know that the gaps are equal to len(chunk).</p>

<p><strong>EDIT:</strong>
However, leading and trailing zeros do not represent gaps. i.e. [0,0,1,2] represents one continuous chunk.</p>

<p>Any help much appreciated.</p>
