Find starting and ending indices of list chunks satisfying given condition

2024/11/15 11:00:29

I am trying to find the start and stop indices of chunks of positive numbers in a list.

cross = [7,5,8,0,0,0,0,2,5,8,0,0,0,0,8,7,9,3,0,0,0,3,2,1,4,5,0,0,0,7,5] 

For the given example input, the desired output is:

[(0, 2), (7, 9), (14, 17), (21, 25), (29, 30)]
Answer

How about using some flags to track where you are in the checking process and some variables to hold historical info?

This is not super elegant code but it is fairly simple to understand I think and fairly robust for the use case you gave.

My code

cross = [7,5,8,0,0,0,0,2,5,8,0,0,0,0,8,7,9,3,0,0,0,3,2,1,4,5,0,0,0,7,5] 
foundstart = False
foundend = False
startindex = 0
endindex = 0
for i in range(0, len(cross)):if cross[i] != 0:if not foundstart:foundstart = Truestartindex = ielse:if foundstart:foundend = Trueendindex = i - 1if foundend:print(startindex, endindex)foundstart = Falsefoundend = Falsestartindex = 0endindex = 0if foundstart:print(startindex, len(cross)-1)

Output

0 2
7 9
14 17
21 25
29 30
https://en.xdnf.cn/q/120459.html

Related Q&A

How can I Scrape Business Email Contact with python?

this morning I wanted to create a little Software/Script in Python, it was 6am when I started and now Im about to become crazy because its 22pm and I have nothing that works.So basically, I want to do …

Python class that works as list of lists

Im trying to create a python class that can work as a list of lists. However, all Ive managed to develop so far is,class MyNestedList(list): ...Im aware that the above code will work as,my = MyNestedLi…

GPA Python Assignment [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Tuple Errors Python

I opened Python and attempted to run the following script (btw, this script is what was directly given to me, I havent edited it in any way as its part of an assignment aside from entering the username…

Flatten list of lists within dictionary values before processing in Pandas

Issue:If I need to flatten a list of lists I use something like this list comprehension to flatten into a single list:[item for sublist in l for item in sublist]I have a dictionary where some of the va…

how to analyse and predict(machine learning) a time series data set using scikit-learn for python

i got data-set like this i need to analyse and predict the status column. This is just 2 entrees from the training data set. In this data set there is heart rate pattern(which is collected in 1 second …

Datetime - Strftime and Strptime

Date = datetime.datetime.today().strftime("%d %B %Y") x = datetime.datetime.strptime(Date , "%d %B %Y")returns:2018-05-09 00:00:00instead of: 9 May 2018, what am I doing wrong? Ess…

Subset sum overlapping dilemma recursively

Im studying recursive logic that one of the problems is subset sum. AFAI read, there are overlaps when it is run by recursion. But, I cannot figure out how there are. Also, I read that to overcome the …

Python - download video from indirect url

I have a link like thishttps://r9---sn-4g57knle.googlevideo.com/videoplayback?id=10bc30daeba89d81&itag=22&source=picasa&begin=0&requiressl=yes&mm=30&mn=sn-4g57knle&ms=nxu&a…

Python trading logic

Heres a simple code for downloading daily stock data and computing Bollinger band indicator, but what I am not able to do is set up a logic for generating a buy and sell signal. Can someone help me wit…