Open In App

Python | Record Similar tuple occurrences

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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}

Time complexity: O(nlogn), where n is the length of the test_list. The map() + Counter() + sorted takes O(nlogn) time
Auxiliary Space: O(n), extra space of size n is required

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}

Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list

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}

Method #4: Using defaultdict

Use defaultdict container from the collections module to group similar tuples and their occurrences.

Step-by-step approach:

  • Import defaultdict from collections module.
  • Initialize the defaultdict with the default value as 0.
  • Loop through the tuples in the given list.
  • Sort each tuple and convert it to a tuple.
  • Increment the count of the sorted tuple in the defaultdict.
  • Return the defaultdict as the result.

Below is the implementation of the above approach:

Python3




from collections import defaultdict
 
# 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))
 
# using defaultdict to group similar tuples
res = defaultdict(int)
for tup in test_list:
    sorted_tup = tuple(sorted(tup))
    res[sorted_tup] += 1
 
# printing result
print("The frequency of like tuples : " + str(dict(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}

Time complexity: O(nlogn), where n is the number of tuples in the given list. Sorting the tuples takes O(nlogn) time, and incrementing the count in the defaultdict takes O(1) time.
Auxiliary space: O(n), where n is the number of tuples in the given list. The defaultdict and the sorted tuples are stored in memory.

Method #5: Using set() and count()

  1. Create an empty dictionary called freq_dict to store the frequency of each tuple
  2. Loop through the test_list
  3. For each tuple in test_list, convert it to a set using set() and store it in a variable tup_set
  4. Check if tup_set is already in freq_dict
  5. If tup_set is not in freq_dict, add it with a count of 1
  6. If tup_set is already in freq_dict, increment the count by 1
  7. Print the resulting dictionary freq_dict

Python3




# Python3 code to demonstrate working of
# Record Similar tuple occurrences
# Using set() and count()
 
# 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 set() and count()
freq_dict = {}
for tup in test_list:
    tup_set = frozenset(tup)
    if tup_set not in freq_dict:
        freq_dict[tup_set] = 1
    else:
        freq_dict[tup_set] += 1
 
# printing result
print("The frequency of like tuples : " + str(freq_dict))


Output

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

Time complexity: O(n) where n is the length of test_list. This is because we loop through test_list only once.
Auxiliary space: O(n) to store freq_dict.

Method #6: Using Dictionary

  • Initialize an empty dictionary named res.
  • Loop through each tuple in the given test_list.
  • Convert each tuple to a sorted tuple.
  • If the sorted tuple already exists in the dictionary res, increment its count value.
  • Otherwise, add the sorted tuple as a new key to the dictionary with a count value of 1.
  • Return the dictionary res

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))
 
res = dict()
for i in test_list:
    a = tuple(sorted(i))
    if a in res:
        res[a] += 1
    else:
        res[a] = 1
 
# 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}

Time complexity: O(nlogn) (sorting each tuple)
Auxiliary space: O(n) (creating a dictionary to store the counts)

Method #7: Using reduce():

  1. Import the defaultdict class from the collections module.
  2. Initialize the list of tuples.
  3. Define a lambda function to be used with the reduce() method. This lambda function will take two arguments, a dictionary and a tuple. It will check if the sorted tuple is in the dictionary, if it is not it will create a new key and set its value to 1, if it is in the dictionary it will increment the value by 1.
  4. Use the reduce() method to apply the lambda function to each tuple in the list, and accumulate the result in a defaultdict with an initial value of zero.
  5. Print the resulting dictionary.
     

Python3




from collections import defaultdict
from functools import reduce
 
# 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))
 
# using defaultdict and reduce to group similar tuples
res = reduce(lambda d, t: d.update({tuple(sorted(t)): d.get(tuple(sorted(t)), 0) + 1}) or d, test_list, defaultdict(int))
 
# printing result
print("The frequency of like tuples : " + str(dict(res)))
#This code is contributed by Rayudu


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}

Time Complexity:

Sorting the tuples takes O(k log k) time, where k is the length of the tuple.
Since the lambda function is applied to each tuple in the list, the time complexity of the reduce() method is O(n), where n is the length of the list.
Therefore, the total time complexity of this algorithm is O(n k log k).
Space Complexity:

We use a defaultdict to store the frequency of similar tuples, so the space complexity of this algorithm is O(m), where m is the number of distinct sorted tuples in the list. In the worst case scenario, where all tuples are distinct, the space complexity is O(n).

Method #8 : Using sort() and operator.countOf() methods

  1. Identified the similar tuples from list using sort(),list(),tuple() methods and for loop and store in b list
  2. And create a dictionary res with keys as tuples and values are count of these tuples in b(using operator.countOf())
  3. Display res

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)
import operator
for i in x:
    res[i] = operator.countOf(b,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}

Time Complexity: O(N log N) N –  length of tuples list
Auxiliary Space : O(N) N – length of dictionary



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads