比较 Python 列表中重叠元组的值
我有两个列表,假设元组的 k
和 m
具有起始值和结束值,即范围。我需要检查 k
和 m
之间是否有任何值重叠,如果是,那么我必须增加一个变量,比如说 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果忽略单个值就可以了,看看这是不是你想要的效果:
If you ignore the single value it will work, see if this is the effect you want: