Python – Count similar pair in Dual List
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)) |
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 #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)) |
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)]