Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Elements frequency in Tuple Matrix

Python – Elements frequency in Tuple Matrix

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

  1. Converted the tuple matrix to list using nested for loops and extend() method
  2. Created a new list with unique elements of above list
  3. Initiated a for loop to create a dictionary with key as unique element and value as it’s count
  4. 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))


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

  1. Converted the tuple matrix to list using nested for loops and extend() method
  2. Created a new list with unique elements of above list
  3. Initiated a for loop to create a dictionary with key as unique element and value as it’s count(using operator.countOf())
  4. 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))


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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments