I have lists of tuples and I cut each of the lists into two parts at the point when tuples = (x, y=-1)
This is the code:
testList = [
[(0.0, -1.0), (0.5, -0.002), (2.0, -0.1613676), (2.5, 1.08492417852)],
[(0.0, -0.99604032), (0.5, -1.0), (2.0, -0.1613832766676), (2.5, 1.0849852)],
[(4.5, 0.154484), (5.0, -1.0), (5.5, -0.34), (6.0, -0.44)],
[(88.5, 3127.7763767), (89.0, 13.449714), (90.0, -1.0)]]
def c_slice(lst):
for slst in lst:
start = 0
for idx,(_,y) in enumerate(slst):
if y == -1:
yield [slst[start:idx+1], slst[idx:]]
break
out = list(c_slice(testList))
print(out[0])
# [[(0.0, -1.0)], [(0.0, -1.0), (0.5, -0.002), (2.0, -0.1613676), (2.5, 1.08492417852)]]
print(out[1])
# [[(0.0, -0.99604032), (0.5, -1.0)], [(0.5, -1.0), (2.0, -0.1613832766676), (2.5, 1.0849852)]]
print(out[2])
# [[(4.5, 0.154484), (5.0, -1.0)], [(5.0, -1.0), (5.5, -0.34), (6.0, -0.44)]]
print(out[3])
# [[(88.5, 3127.7763767), (89.0, 13.449714), (90.0, -1.0)], [(90.0, -1.0)]]
How could I set some limits to my result? For example,
- If the first list in the result doesn't have (x, y>0), add (0, 0) into the beginning of the list. (Like out[0][0] and out[1][0]).
- If the second list in the result doesn't have (x, y>0), add (90, 0) into the end of the list. (Like out[2][1] and out[3][1])
p.s. The position to add tuples is based on the x coordinate. For example, (0, 0) has the smallest x, so it is always added at the beginning of the list. In contrast, (90, 0) has the biggest x coordinate, so it is always added at the last of the list.
I am trying to get this:
print(out[0])
# [[(0.0, 0.0), (0.0, -1.0)], [(0.0, -1.0), (0.5, -0.002), (2.0, -0.1613676), (2.5, 1.08492417852)]]
print(out[1])
# [[(0.0, 0.0), (0.0, -0.99604032), (0.5, -1.0)], [(0.5, -1.0), (2.0, -0.1613832766676), (2.5, 1.0849852)]]
print(out[2])
# [[(4.5, 0.154484), (5.0, -1.0)], [(5.0, -1.0), (5.5, -0.34), (6.0, -0.44), (90.0, 0.0)]]
print(out[3])
# [[(88.5, 3127.7763767), (89.0, 13.449714), (90.0, -1.0)], [(90.0, -1.0), (90.0, 0.0)]]