I'm still not entirely sure why sell result cannot start as an empty list. And why we add and then delete a zero. I'll play with it and figure it out. Thanks.
I have the line if SellResult[-1] < bn
inside the BuyList for loop, because that way it will only cycle through the SellList to compare the current buy value IF the current buy value is more than the previous sell value (SellResult[-1] = last number in SellResult).
If SellResult starts as an empty list, then calling the last index of it will result in an error of "list index out of range". All I did was start it off with a zero so that this if statement wouldn't cause errors with the SellResult right off the bat, then I just removed that zero at the end.
You can write it so that you don't need to start SellResult off with a value in it, but I did just so the code would look simpler.
Here's that other way with SellResult starting off blank:
BuyList = [1,2,3,4,9,11]
SellList = [0,7,8,10,12,15]
BuyResult = []
SellResult = []
def cycleSellList(bn):
global BuyResult, SellResult
for sn in SellList:
if sn > bn:
BuyResult.append(bn)
SellResult.append(sn)
break
for bn in BuyList:
if SellResult: # if it contains values
if SellResult[-1] < bn: # last number in it < bn
cycleSellList(bn)
else: # no values in SellResult yet.
cycleSellList(bn)
"""
My result:
1:7, 9:10, 11:12
"""
Or if you don't want …