比较 Python 列表中重叠元组的值

发布于 2025-01-14 13:14:02 字数 937 浏览 3 评论 0原文

我有两个列表,假设元组的 km 具有起始值和结束值,即范围。我需要检查 km 之间是否有任何值重叠,如果是,那么我必须增加一个变量,比如说 tp。在列表k中,如果列表m中没有找到重叠元组,则必须增加另一个变量fp。同样,在列表m中,如果在k中没有找到重叠元组,则增加另一个变量fn

以下是列表中的值:

k = [(76, 166), (273, 384), (472, 659), (820, 860), (970, 999), (1129, 1180)]

m = [(46, 125), (428, 507), (620, 699)]

我尝试了以下操作,但它给出了错误 int object is not subscriptable

k = [(76, 166), (273, 384), (472, 659), (820, 860), (970, 999), (1129, 1180)]

m = [(46, 125), (428, 507), (620, 699)]

tp = fp = fn = 0

for x in k:
    for y in m:
        if((y[0]>x[0]) and (y[0]<x[1])):
            tp = tp + 1
        elif((y[1]>x[0]) and (y[1]<x[1])):
            tp = tp + 1
        else:
            fp = fp + 1
print("tp: ", tp)
print("fp: ", fp)

I have got two lists let's say k and m of tuples that have start and end values i.e., range. I need to check whether any values overlap between k and m, if yes then I have to increment a variable let's say tp. In list k, if no overlapping tuple is found in list m, then have to increment another variable fp. Likewise, in list m, if no overlapping tuple is found in k, then increment another variable fn.

Here are the values in the lists:

k = [(76, 166), (273, 384), (472, 659), (820, 860), (970, 999), (1129, 1180)]

m = [(46, 125), (428, 507), (620, 699)]

I tried the following but it gives the error int object is not subscriptable:

k = [(76, 166), (273, 384), (472, 659), (820, 860), (970, 999), (1129, 1180)]

m = [(46, 125), (428, 507), (620, 699)]

tp = fp = fn = 0

for x in k:
    for y in m:
        if((y[0]>x[0]) and (y[0]<x[1])):
            tp = tp + 1
        elif((y[1]>x[0]) and (y[1]<x[1])):
            tp = tp + 1
        else:
            fp = fp + 1
print("tp: ", tp)
print("fp: ", fp)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白芷 2025-01-21 13:14:02

如果忽略单个值就可以了,看看这是不是你想要的效果:

k = [(76, 166), (273, 384), (472, 659), (820, 860), (970, 999), (1129, 1180)]

m = [(46, 125), (428, 507), (620, 699)]

tp = fp = fn = 0

for x in k:
    for y in m:
        if (x[0] < y[0] < x[1]) or (x[0] < y[1] < x[1]):
            tp += 1
        else:
            fp += 1
print("tp: ", tp)
print("fp: ", fp)

If you ignore the single value it will work, see if this is the effect you want:

k = [(76, 166), (273, 384), (472, 659), (820, 860), (970, 999), (1129, 1180)]

m = [(46, 125), (428, 507), (620, 699)]

tp = fp = fn = 0

for x in k:
    for y in m:
        if (x[0] < y[0] < x[1]) or (x[0] < y[1] < x[1]):
            tp += 1
        else:
            fp += 1
print("tp: ", tp)
print("fp: ", fp)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文