Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,563 questions

56,781 answers

573 users

How to calculate the percentage similarity of two lists in Python

3 Answers

0 votes
def similarity_based_on_exact_position(lst1, lst2):
    matches = sum(x == y for x, y in zip(lst1, lst2))
     
    return matches / max(len(lst1), len(lst2)) * 100
 
lst1 = [1, 2, 3]
lst2 = [1, 4, 3]
 
print("{:.2f}%".format(similarity_based_on_exact_position(lst1, lst2)))
 
 
  
'''
run:
  
66.67%
 
'''

 



answered Jan 26 by avibootz
edited Jan 26 by avibootz
0 votes
def similarity_based_on_intersection_items(lst1, lst2):
    intersected_items = set(lst1).intersection(lst2)

    similarity_percentage = (len(intersected_items) / ((len(lst1) + len(lst2)) / 2)) * 100
    
    return similarity_percentage

lst1 = [1, 2, 3]
lst2 = [1, 4, 3]

print("{:.2f}%".format(similarity_based_on_intersection_items(lst1, lst2)))


 
'''
run:
 
66.67%

'''

 



answered Jan 26 by avibootz
0 votes
from difflib import SequenceMatcher

def similarity_fuzzy(lst1, lst2):
    return SequenceMatcher(None, lst1, lst2).ratio() * 100


lst1 = ["abc","xzz", "rws"]
lst2 = ["abc","yzz", "rwq"]

print("{:.2f}%".format((similarity_fuzzy(lst1, lst2))))


  
'''
run:
  
33.33%
 
'''

 



answered Jan 26 by avibootz
...