简体   繁体   中英

Adding tuples in lists in Python

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,

  1. 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]).
  2. 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])

ps 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)]]

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)

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM