如何计算Python 2范围之间重叠的百分比
我想计算两个范围之间重叠的百分比。
例如:我有两个列表 x 和 y x['567.630','592.927'] y ['593.000', '618.297'] 在这种情况下,我希望输出为 0,因为没有重叠。
x['793.843','802.244'] y ['794.843','803.244'] 在这种情况下,我想要 87% 的输出。
x 和 y 的总范围并不总是相同。可能 x 是 10s,y 只是 8s。
难道有一些包可以执行这些计算吗?
先感谢您!
I would like to calculate the percentage of overlap between 2 ranges.
for example: I have two lists x and y
x ['567.630', '592.927']
y ['593.000', '618.297']
In this case I would like an output of 0 as there is no overlap.
x ['793.843', '802.244']
y ['794.843', '803.244']
In this case I would like an output of 87%.
The total range of x and y are not always the same. It could be that x is 10s and y just 8s.
Could it be that there are some packages that perform these calculations?
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是更短的解决方案,您可以在其中选择要比较重叠的列表。如果您希望例如[0,1]的重叠和[0,2]为100%,则可以从输出中进行最大(x,y)。我更改了y列表以显示这一点。
输出
This is a bit shorter solution, where you can choose from which list you want to compare the overlap. If you want the overlap of e.g. [0,1] and [0,2] to be 100% you can just do max(x,y) from the output. I changed y list to show this.
output
无需额外进口。我假设数据是浮点数字列表而不是字符串。百分比计算为与第二个间隔重叠,对于其他情况,请在实现中取消注释该行。
重叠部分可以进一步压缩如下
No needs for extra imports. I assumed the data to be list of
float
numbers and not of string. The percentage is computed as overlapping over second interval, for the other case uncomment the line in the implementation.The overlapping part can be further compactified as follow