Python - Count similar pair in Dual List
Last Updated :
15 Mar, 2023
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))
OutputThe 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.
Similar Reads
Python - Extract Similar pairs from List Sometimes, while working with Python lists, we can have a problem in which we need to perform extraction of all the elements pairs in list. This kind of problem can have application in domains such as web development and day-day programming. Let's discuss certain ways in which this task can be perfo
5 min read
Python - Total equal pairs in List Given a list, the task is to write a python program to compute total equal digit pairs, i.e extract the number of all elements with can be dual paired with similar elements present in the list. Input : test_list = [2, 4, 5, 2, 5, 4, 2, 4, 5, 7, 7, 8, 3] Output : 4 Explanation : 4, 2 and 5 have 3 occ
7 min read
Python List Counting Programs Python provides various methods, such as count(), dictionary-based counting, and list comprehensions, to efficiently handle counting operations. This collection of Python programs covers different ways to count elements in lists, including counting occurrences, matching elements, frequency analysis,
2 min read
Python - Group similar elements into Matrix Sometimes, while working with Python Matrix, we can have a problem in which we need to perform grouping of all the elements with are the same. This kind of problem can have applications in data domains. Let's discuss certain ways in which this task can be performed. Input : test_list = [1, 3, 4, 4,
8 min read
Python - Pair iteration in list Pair iteration involves accessing consecutive or specific pairs of elements from a list. It is a common task particularly in scenarios such as comparing neighboring elements, creating tuple pairs, or analyzing sequential data. Python provides several ways to iterate through pairs efficiently ranging
2 min read
Python - Count elements in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the count of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: Using len()
5 min read