两个列表的重叠百分比 [英] Percentage Overlap of Two Lists

查看:94
本文介绍了两个列表的重叠百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是最重要的数学问题.假设我在Python中有两个大小不同的列表

This is more of a math problem than anything else. Lets assume I have two lists of different sizes in Python

listA = ["Alice", "Bob", "Joe"]
listB = ["Joe", "Bob", "Alice", "Ken"]

我想找出这两个列表的重叠百分比.列表中的顺序并不重要.找到重叠是很容易的,我已经看过其他有关如何做到这一点的文章,但是我在脑海中无法完全扩展以找出重叠的百分比.如果我按不同顺序比较列表,结果会有所不同吗?最好的方法是什么?

I want to find out what percentage overlap these two lists have. Order is not important within the lists. Finding overlap is easy, I've seen other posts on how to do that but I can't quite extend it in my mind to finding out what percentage they overlap. If I compared the lists in different orders would the result come out differently? What would be the best way of doing this?

推荐答案

从原理上讲,我想说的是,您可能要问两个明智的问题:

From the principal point of view, I'd say that there are two sensible questions you might be asking:

  1. 如果与第一个列表进行比较,重叠的百分比是多少? IE.与第一个列表相比,通用部分有多大?
  2. 第二个列表也是一样.
  3. 如果与"Universe"(即两个列表的并集)相比,重叠的百分比是多少?

当然也可以找到其他含义,并且会有很多含义.总之,您可能应该知道您要解决的问题.

There can surely be found other meanings as well and there would be many of them. All in all you should probably know what problem you're trying to solve.

从编程的角度来看,解决方案很简单:

From programming point of view, the solution is easy:

listA = ["Alice", "Bob", "Joe"]
listB = ["Joe", "Bob", "Alice", "Ken"]

setA = set(listA)
setB = set(listB)

overlap = setA & setB
universe = setA | setB

result1 = float(len(overlap)) / len(setA) * 100
result2 = float(len(overlap)) / len(setB) * 100
result3 = float(len(overlap)) / len(universe) * 100

这篇关于两个列表的重叠百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆