Identify runs in list

Chris Rebert clp2 at rebertia.com
Sat Oct 3 22:36:50 EDT 2009


On Sat, Oct 3, 2009 at 7:21 PM, skorpio11 at gmail.com <skorpio11 at gmail.com> wrote:
> Hi all,
>
> I have a data structure in a list as in: [0 0 0 3 0 5 0 0 0 0 1 0 4 0
> 5 0 0 7 0 0 0 0 0 12 0 0 4]
>
> I would like to extract three list from this data:
>
> 1) runsOfZero: [3 4 5]
> 2) runsOfNonZero: [3 8 4]
> 3) SumOfRunsOfNonZero: [8 17 16]
>
> Any suggestions would be appreciated

Since this sounds like homework, I won't give actual code, but here's
a gameplan:

1. Split the list into sublists based on where the runs of zeros stop and start.
2. Categorize the sublists and place them into lists-of-lists based on
whether they have nonzero entries. To do the categorization, you'll
have to iterate over the original list and track how many previous 0s
you've seen consecutively.
3. Use len() on the nonzero lists to get their length; puts the
results into a list (runsOfNonZero)
4. Use sum() on the nonzero lists to get their sums, and put the
results into another list (SumOfRunsOfNonZero)
5. Use len() on the all-zero lists to get their length and put the
results into a list (runsOfZero)

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list