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
from itertools import chain
from collections import Counter
test_list = [[( 4 , 5 ), ( 3 , 2 )], [( 2 , 2 )], [( 1 , 2 ), ( 5 , 5 )]]
print ( "The original list is : " + str (test_list))
res = dict (Counter(chain( * chain( * test_list))))
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
from itertools import chain
from collections import Counter
test_list = [[( 4 , 5 ), ( 3 , 2 )], [( 2 , 2 )], [( 1 , 2 ), ( 5 , 5 )]]
print ( "The original list is : " + str (test_list))
res = dict (Counter(chain.from_iterable(chain.from_iterable(test_list))))
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
test_list = [[( 4 , 5 ), ( 3 , 2 )], [( 2 , 2 )], [( 1 , 2 ), ( 5 , 5 )]]
print ( "The original list is : " + str (test_list))
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)
print ( "Elements frequency : " + str (res))
|
Output
The 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
import operator
test_list = [[( 4 , 5 ), ( 3 , 2 )], [( 2 , 2 )], [( 1 , 2 ), ( 5 , 5 )]]
print ( "The original list is : " + str (test_list))
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)
print ( "Elements frequency : " + str (res))
|
Output
The 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)))
|
Output
Elements 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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...