Open In App

Python – Count similar pair in Dual List

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with data, we can have a problem in which we receive the dual elements pair and we intend to find pairs of similar elements and it’s frequency. This kind of data is usually useful in data domains. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using Counter() + map() + sorted() + items() 
The combination of above functions can be used to achieve this particular task. In this, we first find frequency using Counter and then link it in a tuple using map(). The sorted() performs task of sorting before using above method.
 

Python3




# Python3 code to demonstrate
# Count Similar pair in dual list
# using Counter() + map() + sorted() + items()
from collections import Counter
 
# initializing list
test_list = [[1, 2], [2, 1], [3, 4], [4, 3], [5, 4]]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# Count Similar pair in dual list
# using Counter() + map() + sorted() + items()
temp = [sorted(ele) for ele in test_list]
res = [(i, j, k) for (i, j), k in Counter(map(tuple, temp)).items()]
 
# printing result
print ("The dual list similarity counts : " + str(res))


Output : 

The original list is : [[1, 2], [2, 1], [3, 4], [4, 3], [5, 4]]
The dual list similarity counts : [(1, 2, 2), (4, 5, 1), (3, 4, 2)]

 

Time Complexity: O(n*nlogn) where n is the number of elements in the list “test_list”.  
Auxiliary Space: O(n), where n is the number of elements in the new res list 

 
Method #2 : Using sum() + list comprehension + groupby() + sorted() 
In this method, the task of counting is performed using sum() and task of getting group is performed using groupby().
 

Python3




# Python3 code to demonstrate
# Count Similar pair in dual list
# using sum() + list comprehension + groupby() + sorted()
from itertools import groupby
 
# initializing list
test_list = [[1, 2], [2, 1], [3, 4], [4, 3], [5, 4]]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# Count Similar pair in dual list
# using sum() + list comprehension + groupby() + sorted()
res = [(*temp, sum(1 for idx in elements))
      for temp, elements in groupby(test_list, key = lambda j : sorted(j))]
 
# printing result
print ("The dual list similarity counts : " + str(res))


Output : 

The original list is : [[1, 2], [2, 1], [3, 4], [4, 3], [5, 4]]
The dual list similarity counts : [(1, 2, 2), (4, 5, 1), (3, 4, 2)]

 

Method #3: Using dictionary to count occurrences
In this method, we can use a dictionary to store the count of occurrences of each tuple in the dual list.

Python3




# Python3 code to demonstrate
# Count Similar pair in dual list
# using dictionary to count occurrences
 
# initializing list
test_list = [[1, 2], [2, 1], [3, 4], [4, 3], [5, 4]]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# Count Similar pair in dual list
# using dictionary to count occurrences
counts = {}
for ele in test_list:
    sorted_ele = tuple(sorted(ele))
    if sorted_ele in counts:
        counts[sorted_ele] += 1
    else:
        counts[sorted_ele] = 1
 
result = [(key[0],key[1], value) for key, value in counts.items()]
 
# printing result
print ("The dual list similarity counts : " + str(result))


Output

The original list is : [[1, 2], [2, 1], [3, 4], [4, 3], [5, 4]]
The dual list similarity counts : [(1, 2, 2), (3, 4, 2), (4, 5, 1)]

Time Complexity: O(n), where n is the length of the input list.
Space Complexity: O(n), as the number of unique elements in the list determines the size of the dictionary.

Explanation:
In this method, we create an empty dictionary counts and loop through each element in the input list. For each element, we sort it and convert it to a tuple. Then, we check if the sorted and tuple-ized element exists in the dictionary. If it does, we increment its count. If it doesn’t, we set its count to 1. Finally, we convert the dictionary to a list of tuples and return the result.



Last Updated : 15 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads