Reputation: 503
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,
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)]]
Upvotes: 2
Views: 90
Reputation: 480
I coded this solution:
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,(x,y) in enumerate(slst):
if y == -1:
if x <= len(slst)/2:
yield [[(x, 0)] + slst[start:idx+1], slst[idx:]]
else:
yield [slst[start:idx+1] + [(x, 0)], slst[idx:]]
break
out = list(c_slice(testList))
print(out[0])
print(out[1])
print(out[2])
print(out[3])
Upvotes: 0
Reputation: 48
you can try it like this
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(rows):
for row in rows:
for idx, (_, y) in enumerate(row):
if y == -1:
start = row[:idx + 1]
end = row[idx:]
start = [(0, 0)] + start if all([y < 0 for _, y in start]) else start
end = end + [(90, 0)] if all([y < 0 for _, y in end]) else end
yield [start, end]
break
out = list(c_slice(testList))
for i in out:
print(i)
Upvotes: 1