Skip to content
Related Articles

Related Articles

Python | Record Similar tuple occurrences

View Discussion
Improve Article
Save Article
  • Last Updated : 22 Sep, 2022
View Discussion
Improve Article
Save Article

Sometimes, while working with data, we can have a problem in which we need to check the occurrences of records that occur at similar times. This has applications in the web development domain. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using map() + Counter() + sorted 

The combination of the above functionalities can be used to perform this task. In this, before feeding data to Counter(), for counting occurrences, sort the data to make all unordered tuple ordered to count similar ones as one. 

Python3




# Python3 code to demonstrate working of
# Record Similar tuple occurrences
# Using Counter() + map() + sorted
from collections import Counter
 
# initialize list
test_list = [(3, 1), (1, 3), (2, 5), (5, 2), (6, 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Record Similar tuple occurrences
# Using Counter() + map() + sorted
res = dict(Counter(tuple(ele) for ele in map(sorted, test_list)))
 
# printing result
print("The frequency of like tuples : " + str(res))

Output : 

The original list is : [(3, 1), (1, 3), (2, 5), (5, 2), (6, 3)]
The frequency of like tuples : {(2, 5): 2, (1, 3): 2, (3, 6): 1}

Method #2: Using frozenset() + Counter() The combination of above functions can be used to perform this particular task. In this, the task performed by sorted and map() is performed by the frozenset() which orders the tuples internally. 

Python3




# Python3 code to demonstrate working of
# Record Similar tuple occurrences
# Using frozenset() + Counter()
from collections import Counter
 
# initialize list
test_list = [(3, 1), (1, 3), (2, 5), (5, 2), (6, 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Record Similar tuple occurrences
# Using frozenset() + Counter()
res = dict(Counter(tuple(frozenset(ele)) for ele in test_list))
 
# printing result
print("The frequency of like tuples : " + str(res))

Output : 

The original list is : [(3, 1), (1, 3), (2, 5), (5, 2), (6, 3)]
The frequency of like tuples : {(2, 5): 2, (1, 3): 2, (3, 6): 1}

Method #3:  Using sort() and count() methods

Python3




# Python3 code to demonstrate working of
# Record Similar tuple occurrences
 
# initialize list
test_list = [(3, 1), (1, 3), (2, 5), (5, 2), (6, 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
x = []
b = []
res = dict()
for i in test_list:
    a = list(i)
    a.sort()
    a = tuple(a)
    if a not in x:
        x.append(a)
    b.append(a)
for i in x:
    res[i] = b.count(i)
 
# printing result
print("The frequency of like tuples : " + str(res))

Output

The original list is : [(3, 1), (1, 3), (2, 5), (5, 2), (6, 3)]
The frequency of like tuples : {(1, 3): 2, (2, 5): 2, (3, 6): 1}

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!