Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Cumulative Row Frequencies in List

Python – Cumulative Row Frequencies in List

Given Matrix, the task is to write a Python program to get total counts of occurrences from the row.

Input : test_list = [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]], ele_list = [1, 2, 7] 
Output : [2, 2, 2, 5] 
Explanation : 2 occurs 2 times in row 1 and so on.

Input : test_list = [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4]], ele_list = [1, 2, 7] 
Output : [2, 2] 
Explanation : 2 occurs 2 times in row 1 and so on. 
 

Method #1 : Using Counter() + list comprehension

In this, we perform the task of getting all the frequencies of all rows, and then check in the list occurrence of the required element from the row, sum() is used to get a summation of extracted frequencies in rows.

Python3




# Python3 code to demonstrate working of
# Cumulative Row Frequencies
# Using Counter() + list comprehension
from collections import Counter
 
# initializing list
test_list = [[10, 2, 3, 2, 3],
             [5, 5, 4, 7, 7, 4],
             [1, 2], [1, 1, 2, 2, 2]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing ele_list
ele_list = [1, 2, 7]
 
# getting each rows counter
freqs = [Counter(ele) for ele in test_list]
 
# getting summation of present values
res = [sum([freq[wrd] for wrd in ele_list if wrd in freq]) for freq in freqs]
 
# printing result
print("Cumulative Frequencies : " + str(res))


Output:

The original list is : [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]] 
Cumulative Frequencies : [2, 2, 2, 5]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using sum() + list comprehension

In this, we perform the task of getting summation using sum(), and list comprehension is used to check for elements in the check list and iterate through rows.

Python3




# Python3 code to demonstrate working of
# Cumulative Row Frequencies
# Using sum() + list comprehension
 
# initializing list
test_list = [[10, 2, 3, 2, 3],
             [5, 5, 4, 7, 7, 4],
             [1, 2], [1, 1, 2, 2, 2]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing ele_list
ele_list = [1, 2, 7]
 
# getting summation
res = [sum(ele in ele_list for ele in sub) for sub in test_list]
 
# printing result
print("Cumulative Frequencies : " + str(res))


Output:

The original list is : [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]] 
Cumulative Frequencies : [2, 2, 2, 5]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3 : Using  itertools:

Algorithm:

  1. Initialize the test list and the ele_list.
  2. Use a list comprehension to iterate through each sublist in the test_list.
  3. Use another list comprehension to count the number of elements that are present in the ele_list in the current sublist.
  4. Append the count to the result list.
  5. Print the result.

Python3




import itertools
 
test_list = [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]]
ele_list = [1, 2, 7]
# printing original list
print("The original list is : " + str(test_list))
res = [sum(1 for ele in sublist if ele in ele_list) for sublist in test_list]
 
print("Cumulative Frequencies : ", res)
#This code is contrinuted by Pushpa.


Output

The original list is : [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]]
Cumulative Frequencies :  [2, 2, 2, 5]

Time complexity: O(n^2), where n is the length of the longest sublist in the test_list. This is because we are using two nested loops to iterate through each sublist and its elements.

Auxiliary Space: O(n), where n is the length of the longest sublist in the test_list. This is because we are creating a result list to store the cumulative frequencies of the elements in the ele_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