Python | Percentage similarity of lists
Last Updated :
10 May, 2023
Sometimes, while working with Python list, we have a problem in which we need to find how much a list is similar to other list. The similarity quotient of both the list is what is required in many scenarios we might have. Let's discuss a way in which this task can be performed.
Method 1: 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.
Python3
# Python3 code to demonstrate working of
# Percentage similarity of lists
# using "|" operator + "&" operator + set()
# initialize lists
test_list1 = [1, 4, 6, 8, 9, 10, 7]
test_list2 = [7, 11, 12, 8, 9]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# Percentage similarity of lists
# using "|" operator + "&" operator + set()
res = len(set(test_list1) and set(test_list2)) / float(len(set(test_list1) or set(test_list2))) * 100
# printing result
print("Percentage similarity among lists is : " + str(res))
OutputThe original list 1 is : [1, 4, 6, 8, 9, 10, 7]
The original list 2 is : [7, 11, 12, 8, 9]
Percentage similarity among lists is : 71.42857142857143
Time Complexity: O(m*n) where n is the number of elements in the list “test_lists”.
Auxiliary Space: O(1), constant extra space is required
Method#2 : Using set() + intersection()+ union
Find the intersection and union of the two lists and then we calculate the percentage.
Step-by-step approach:
- First, define the two original lists.
- Use the set data type to find the number of common elements in both lists.
- Do this by taking the intersection of the two sets, and finding its length.
- Use the set data type again to find the total number of unique elements in both lists.
- Do this by taking the union of the two sets, and finding its length.
- Calculate the percentage similarity by dividing the number of common elements by the total number of unique elements, and multiplying the result by 100.
- Use the format method to print the result with two decimal places. The resulting output shows that the two lists have a similarity of 33.33%.
Python3
# Define the two original lists
original_list1 = [1, 4, 6, 8, 9, 10, 7]
original_list2 = [7, 11, 12, 8, 9]
# Find the number of common elements in both lists
common_elements = set(original_list1).intersection(set(original_list2))
num_common_elements = len(common_elements)
# Find the total number of unique elements in both lists
total_elements = set(original_list1).union(set(original_list2))
num_total_elements = len(total_elements)
# Calculate the percentage similarity
percentage_similarity = (num_common_elements / num_total_elements) * 100
# Print the result
print("Percentage similarity among lists is : {:.2f}".format(percentage_similarity))
OutputPercentage similarity among lists is : 33.33
Time complexity: O(n), where n is the total number of elements in both lists. This is because we are creating sets from both lists, which takes O(n) time, and then we are performing set operations on these sets, which take constant time on average. Finally, we are doing simple arithmetic calculations, which also take constant time. Therefore, the overall time complexity is O(n).
Auxiliary Space: O(n), where n is the total number of elements in both lists. This is because we are creating two sets, one for each list, which can potentially contain all the elements in the list. Therefore, the space taken by the sets is proportional to the total number of elements in both lists. Additionally, we are also creating some variables to store the number of common and total elements, which take constant space. Therefore, the overall space complexity is O(n).
Method #3 : Using Counter() from collections module:
Step-by-step approach:
- Import Counter from collections module.
- Initialize two Counter objects for each input list.
- Calculate the intersection of the two Counter objects to get a new Counter object with the count of common elements in the two lists.
- Get the elements of the common elements Counter object as a list.
- Calculate the percentage similarity as the ratio of the length of the common elements list to the length of the set of all elements in both input lists.
- Print the percentage similarity
Python3
from collections import Counter
original_list1 = [1, 4, 6, 8, 9, 10, 7]
original_list2 = [7, 11, 12, 8, 9]
counter1 = Counter(original_list1)
counter2 = Counter(original_list2)
common_elements = list((counter1 & counter2).elements())
percentage_similarity = len(common_elements) / len(set(original_list1 + original_list2)) * 100
print("Percentage similarity among lists is : {:.2f}".format(percentage_similarity))
#This code is contributed by Jyothi pinjala
OutputPercentage similarity among lists is : 33.33
Time Complexity:
The time complexity of this algorithm is O(n), where n is the total number of elements in both input lists. The Counter objects are created in O(n) time and the intersection operation also takes O(n) time in the worst case.
Auxiliary Space:
The space complexity of this algorithm is O(n), where n is the total number of elements in both input lists. The Counter objects are created to store the count of each element in each input list, and the common elements list can have a maximum of n elements.
Method #4: Using list comprehension and set()
- Define the two original lists
- Create a list comprehension to get all the common elements in both lists
- Find the total number of unique elements in both lists using set union
- Calculate the percentage similarity using the common elements count and total elements count
- Print the result
Python3
# Define the two original lists
original_list1 = [1, 4, 6, 8, 9, 10, 7]
original_list2 = [7, 11, 12, 8, 9]
# Create a list comprehension to get all the common elements in both lists
common_elements = [element for element in original_list1 if element in original_list2]
# Find the total number of unique elements in both lists using set union
total_elements = set(original_list1).union(set(original_list2))
num_total_elements = len(total_elements)
# Calculate the percentage similarity using the common elements count and total elements count
percentage_similarity = (len(common_elements) / num_total_elements) * 100
# Print the result
print("Percentage similarity among lists is : {:.2f}".format(percentage_similarity))
OutputPercentage similarity among lists is : 33.33
Time complexity: O(n+m), where n and m are the lengths of the two lists
Auxiliary space: O(p), where p is the number of unique elements in both lists
Similar Reads
Python | Sort a list of percentage Given a list of percentage, write a Python program to sort the given list in ascending order. Let's see different ways to do the task. Code #1: Chops '%' in string and convert it into float. Python3 # Python code to sort list of percentage # List initialization Input =['2.5 %', '6.4 %', '91.6 %', '1
3 min read
Python - Alternate elements Similarity Given list of elements, check if all the alternate elements are equal to K. Input : test_list = [5, 3, 5, 2, 5, 8, 9], K = 5 Output : False Explanation : 9 != 5, hence False. Input : test_list = [4, 3, 4, 2, 4], K = 4 Output : True Explanation : All alternates equal to 4. Method #1 : Using loop This
4 min read
Python | Extract similar index elements Sometimes, while working with Python data, we can have a problem in which we require to extract the values across multiple lists which are having similar index values. This kind of problem can come in many domains. Let's discuss certain ways in which this problem can be solved. Method #1 : Using loo
6 min read
Python | Test list element similarity Given a list, your task is to determine the list is K percent same i.e has element that is populated more than K % times. Given below are few methods to solve the task. Method #1 Using collections.Counter Python3 # Python3 code to demonstrate # to check whether the list # K percent same or not from
4 min read
Similarity Metrics of Strings - Python In Python, we often need to measure the similarity between two strings. For example, consider the strings "geeks" and "geeky" âwe might want to know how closely they match, whether for tasks like comparing user inputs or finding duplicate entries. Let's explore different methods to compute string si
3 min read