Python - Elements frequency in Tuple Matrix
Last Updated :
08 May, 2023
Sometimes, while working with Python Tuple Matrix, we can have a problem in which we need to get the frequency of each element in it. This kind of problem can occur in domains such as day-day programming and web development domains. Let's discuss certain ways in which this problem can be solved.
Input : test_list = [[(4, 5), (3, 2)], [(2, 2)]]
Output : {4: 1, 5: 1, 3: 1, 2: 3}
Input : test_list = [[(4, 5), (3, 2)]]
Output : {4: 1, 5: 1, 3: 1, 2: 1}
Method #1 : Using nested chain() + "*" operator + Counter()
The combination of the above functions can be used to solve this problem. In this, we perform the task of getting frequency using Counter() and nested chain to cater the nestings, and the "*" operator is used to perform unpacking and packing of each element.
Python3
# Python3 code to demonstrate working of
# Elements frequency in Tuple Matrix
# Using nested chain() + "*" operator + Counter()
from itertools import chain
from collections import Counter
# initializing lists
test_list = [[(4, 5), (3, 2)], [(2, 2)], [(1, 2), (5, 5)]]
# printing original list
print("The original list is : " + str(test_list))
# Elements frequency in Tuple Matrix
# Using nested chain() + "*" operator + Counter()
res = dict(Counter(chain(*chain(*test_list))))
# printing result
print("Elements frequency : " + str(res))
Output : The original list is : [[(4, 5), (3, 2)], [(2, 2)], [(1, 2), (5, 5)]]
Elements frequency : {4: 1, 5: 3, 3: 1, 2: 4, 1: 1}
Time complexity: O(m*n), because it performs the same number of iterations as the original code.
Auxiliary space: O(m*n) as well, because it creates a dictionary with m * n keys and a list of m * n elements
Method #2: Using chain.from_iterables() + Counter()
The combination of the above functions can be used to solve this problem. In this, we perform the task of packing, unpacking, and flattening using chain.from_iterables().
Python3
# Python3 code to demonstrate working of
# Elements frequency in Tuple Matrix
# Using chain.from_iterables() + Counter()
from itertools import chain
from collections import Counter
# initializing lists
test_list = [[(4, 5), (3, 2)], [(2, 2)], [(1, 2), (5, 5)]]
# printing original list
print("The original list is : " + str(test_list))
# Elements frequency in Tuple Matrix
# Using chain.from_iterables() + Counter()
res = dict(Counter(chain.from_iterable(chain.from_iterable(test_list))))
# printing result
print("Elements frequency : " + str(res))
Output : The original list is : [[(4, 5), (3, 2)], [(2, 2)], [(1, 2), (5, 5)]]
Elements frequency : {4: 1, 5: 3, 3: 1, 2: 4, 1: 1}
Time Complexity: O(n^2), where n is the total number of tuples in the input "test_list".
Auxiliary Space: O(n)
Method #3 : Using extend() and count() methods
Approach
- Converted the tuple matrix to list using nested for loops and extend() method
- Created a new list with unique elements of above list
- Initiated a for loop to create a dictionary with key as unique element and value as it's count
- Displayed the dictionary
Python3
# Python3 code to demonstrate working of
# Elements frequency in Tuple Matrix
# initializing lists
test_list = [[(4, 5), (3, 2)], [(2, 2)], [(1, 2), (5, 5)]]
# printing original list
print("The original list is : " + str(test_list))
# Elements frequency in Tuple Matrix
res = dict()
x = []
for i in test_list:
for j in i:
x.extend(list(j))
y = list(set(x))
for i in y:
res[i] = x.count(i)
# printing result
print("Elements frequency : " + str(res))
OutputThe original list is : [[(4, 5), (3, 2)], [(2, 2)], [(1, 2), (5, 5)]]
Elements frequency : {1: 1, 2: 4, 3: 1, 4: 1, 5: 3}
Time Complexity : O(N*N)
Auxiliary Space : O(N)
Method #4 : Using extend() and operator.countOf() methods
Approach
- Converted the tuple matrix to list using nested for loops and extend() method
- Created a new list with unique elements of above list
- Initiated a for loop to create a dictionary with key as unique element and value as it's count(using operator.countOf())
- Displayed the dictionary
Python3
# Python3 code to demonstrate working of
# Elements frequency in Tuple Matrix
# initializing lists
import operator
test_list = [[(4, 5), (3, 2)], [(2, 2)], [(1, 2), (5, 5)]]
# printing original list
print("The original list is : " + str(test_list))
# Elements frequency in Tuple Matrix
res = dict()
x = []
for i in test_list:
for j in i:
x.extend(list(j))
y = list(set(x))
for i in y:
res[i] = operator.countOf(x, i)
# printing result
print("Elements frequency : " + str(res))
OutputThe original list is : [[(4, 5), (3, 2)], [(2, 2)], [(1, 2), (5, 5)]]
Elements frequency : {1: 1, 2: 4, 3: 1, 4: 1, 5: 3}
Time Complexity : O(N*N) N -length of extended list(x)
Auxiliary Space : O(N) N - length of dictionary
Method #5: Using defaultdict()
Step-by-step approach:
- Import the defaultdict class from the collections module.
- Initialize an empty defaultdict object with int() as the default value.
- Iterate over the test_list, accessing each nested tuple.
- Iterate over each tuple, accessing each element.
- Increment the count of each element in the defaultdict object.
- Print the resulting dictionary
Python3
from collections import defaultdict
test_list = [[(4, 5), (3, 2)], [(2, 2)], [(1, 2), (5, 5)]]
res = defaultdict(int)
for i in test_list:
for j in i:
for k in j:
res[k] += 1
print("Elements frequency : " + str(dict(res)))
OutputElements frequency : {4: 1, 5: 3, 3: 1, 2: 4, 1: 1}
Time Complexity: O(n^2), where n is the length of the largest nested tuple in the test_list.
Auxiliary Space: O(m), where m is the number of unique elements in the test_list.
Similar Reads
Python - Elements frequency in Tuple Given a Tuple, find the frequency of each element. Input : test_tup = (4, 5, 4, 5, 6, 6, 5) Output : {4: 2, 5: 3, 6: 2} Explanation : Frequency of 4 is 2 and so on.. Input : test_tup = (4, 5, 4, 5, 6, 6, 6) Output : {4: 2, 5: 2, 6: 3} Explanation : Frequency of 4 is 2 and so on.. Method #1 Using def
7 min read
Python - Elements Frequency in Mixed Nested Tuple Sometimes, while working with Python data, we can have a problem in which we have data in the form of nested and non-nested forms inside a single tuple, and we wish to count the element frequency in them. This kind of problem can come in domains such as web development and Data Science. Let's discus
8 min read
Python - Maximum frequency in Tuple Sometimes, while working with Python tuples, we can have a problem in which we need to find the maximum frequency element in the tuple. Tuple, being quite a popular container, this type of problem is common across the web development domain. Let's discuss certain ways in which this task can be perfo
5 min read
Python | Tuple Column element frequency In Python, we need to handle various forms of data and one among them is a list of tuples in which we may have to perform any kind of operation. This particular article discusses the ways of finding the frequency of the Kth element in the list of tuples. Letâs discuss certain ways in which this can
5 min read
Python - Matrix elements Frequencies Counter Sometimes, while working with python Matrix, we can have a problem in which we need to find frequencies of all elements in Matrix. This kind of problem can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using Counter() + sum() + map() The
5 min read