简体   繁体   English

在 Python 中的列表中切片元组

[英]Slicing tuples in lists in Python

I have lists like this:我有这样的清单:

testList = [[(0.0, -0.9960135495794032), (0.5, -1.0), (2.0, -0.16138322487766676), (2.5, 1.0849272141417852)], 
[(4.0, 3.3149805356833015), (4.5, 0.1649293864654484), (5.0, -1.0), (5.5, 0.33841349597101744), (6.0, 4.702347949145297)], 
[(88.5, 3469038127.7763767), (89.0, 1309004102.449714), (90.0, -1.0)]]

I am trying to slice the lists based on the second index in the tuple, for example, I would like the cut each of the lists into two parts at the point when the tuple is (x, y=-1).我正在尝试根据元组中的第二个索引对列表进行切片,例如,我希望在元组为 (x, y=-1) 时将每个列表分成两部分。

This is the result I am trying to get:这是我想要得到的结果:

[[(0.0, -0.9960135495794032), (0.5, -1.0)], [(0.5, -1.0), (2.0, -0.16138322487766676), (2.5, 1.0849272141417852)]]

[[(4.0, 3.3149805356833015), (4.5, 0.1649293864654484), (5.0, -1.0)], [(5.0, -1.0), (5.5, 0.33841349597101744), (6.0, 4.702347949145297)]]

[[(88.5, 3469038127.7763767), (89.0, 1309004102.449714), (90.0, -1.0)], [(90.0, -1.0)]]

How can I make it?我怎样才能做到?

In the first two cases, when you split, you put the (x,y) couple where you split in both parts.在前两种情况下,当你分裂时,你把 (x,y) 对放在你分裂的地方。 For consistency I do the same for the third (hence the second part is not empty).为了保持一致性,我对第三部分做同样的事情(因此第二部分不是空的)。 But it's easy to adapt.但是很容易适应。

testList = [
    [(0.0, -0.9960135495794032), (0.5, -1.0), (2.0, -0.16138322487766676), (2.5, 1.0849272141417852)], 
    [(4.0, 3.3149805356833015), (4.5, 0.1649293864654484), (5.0, -1.0), (5.5, 0.33841349597101744), (6.0, 4.702347949145297)], 
    [(88.5, 3469038127.7763767), (89.0, 1309004102.449714), (90.0, -1.0)]
]

def find_y(a, val):
    for i, (x, y) in enumerate(a):
        if y == val:
            return i
    return -1

def split_y(a):
    k = find_y(a, -1)
    if k < 0:
        return a, []
    else:
        return a[:k + 1], a[k:]

for i, u in enumerate(testList):
    l, r = split_y(u)
    print("left%d = %s" % (i + 1, l))
    print("right%d = %s" % (i + 1, r))

Cautionary note: usually it's a very bad idea to test floating point values for equality.注意事项:通常测试浮点值是否相等是一个非常糟糕的主意。 A more robust test could be for instance:例如,更强大的测试可能是:

def approx_eq(a, b, eps=1e-8):
    return abs(a - b) < eps * max(abs(a), abs(b))

You can try this.你可以试试这个。

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

Output: Output:

out = list(c_slice(testList))

print(out[0])
#[[(0.0, -0.9960135495794032), (0.5, -1.0)],
# [(0.5, -1.0), (2.0, -0.16138322487766676), (2.5, 1.0849272141417852)]]

print(out[1])
#[[(4.0, 3.3149805356833015), (4.5, 0.1649293864654484), (5.0, -1.0)],
# [(5.0, -1.0), (5.5, 0.33841349597101744), (6.0, 4.702347949145297)]]   

print(out[2])
#[[(88.5, 3469038127.7763767), (89.0, 1309004102.449714), (90.0, -1.0)], 
#  [(90.0, -1.0)]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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