Identify runs in list

S

skorpio11

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
 
C

Chris Rebert

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
 
S

skorpio11

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,189
Messages
2,571,015
Members
47,616
Latest member
gijoji4272

Latest Threads

Top