Python | Record Similar tuple occurrences
Last Updated :
08 May, 2023
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))
OutputThe 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)))
OutputThe 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()
- Create an empty dictionary called freq_dict to store the frequency of each tuple
- Loop through the test_list
- For each tuple in test_list, convert it to a set using set() and store it in a variable tup_set
- Check if tup_set is already in freq_dict
- If tup_set is not in freq_dict, add it with a count of 1
- If tup_set is already in freq_dict, increment the count by 1
- 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))
OutputThe 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))
OutputThe 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():
- Import the defaultdict class from the collections module.
- Initialize the list of tuples.
- 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.
- 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.
- 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
OutputThe 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
- Identified the similar tuples from list using sort(),list(),tuple() methods and for loop and store in b list
- And create a dictionary res with keys as tuples and values are count of these tuples in b(using operator.countOf())
- 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))
OutputThe 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
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read