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
Thanks Chris, Not homework but self learning. Also, forgot to mention
that I only count runs greater than 3 (zeros). I will now look over
your suggestions. Thanks again
>> To do the categorization, you'll
>> have to iterate over the original list and track how many previous 0s
>> you've seen consecutively.
Erm, s/categorization/splitting and move this sentence into point #1,
obviously. Mea culpa. :)
Cheers,
Chris
Look at the docs of the groupby function in the itertools module.