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() peforms task of sorting before using above method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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 #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().

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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)]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.