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?
Finally, to calculate the percentage of overlap, the area of overlap between the circles (a) is divided by the area of the small circle (Pi*r 2 ) and multiplied by 100: overlap output 1⁄4 (100*a) / (Pi*r 2 ).
Method : Using "|" operator + "&" operator + set() The method which is formally applied to calculate the similarity among lists is finding the distinct elements and also common elements and computing it's quotient. The result is then multiplied by 100, to get the percentage.
From the principal point of view, I'd say that there are two sensible questions you might be asking:
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
>>> len(set(listA)&set(listB)) / float(len(set(listA) | set(listB))) * 100
75.0
I would calculate the common items out of the total distinct items.
len(set(listA)&set(listB))
returns the common items (3 in your example).
len(set(listA) | set(listB))
returns the total number of distinct items (4).
Multiply by 100 and you get percentage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With