Sometimes while dealing with statistics, we might come across this particular problem in which we require to find the percentage of occurrence of element at particular index in list of list. Let’s discuss certain ways in which this can be done.
Method #1 : Using loop + list comprehension We can perform this particular task using the loop for each list index, and for that we can compute the percentage occurrence of element using the list comprehension logic.
Python3
# Python3 code to demonstrate # index percentage calculation of element # using loop + list comprehension # initializing test list test_list = [[ 3 , 4 , 5 ], [ 2 , 4 , 6 ], [ 3 , 5 , 4 ]] # printing original list print ("The original list : " + str (test_list)) # using loop + list comprehension # index percentage calculation of element res = [] for i in range ( len (test_list[ 1 ])): res.append( len ([j[i] for j in test_list if j[i] = = 4 ]) / len (test_list)) # print result print ("The percentage of 4 each index is : " + str (res)) |
The original list : [[3, 4, 5], [2, 4, 6], [3, 5, 4]] The percentage of 4 each index is : [0.0, 0.6666666666666666, 0.3333333333333333]
Time complexity: O(n*m), where n is the number of sub-lists and m is the length of the test_list.
Auxiliary space: O(n), where n is length of res list.
Method #2 : Using zip() + count() + list comprehension This particular task can also be performed using the combination of above functions. The count and zip function do the task of percentage computation and grouping respectively.
Python3
# Python3 code to demonstrate # index percentage calculation of element # using zip() + count() + list comprehension # initializing test list test_list = [[ 3 , 4 , 5 ], [ 2 , 4 , 6 ], [ 3 , 5 , 4 ]] # printing original list print ("The original list : " + str (test_list)) # using zip() + count() + list comprehension # index percentage calculation of element res = [sub.count( 4 ) / len (sub) for sub in zip ( * test_list)] # print result print ("The percentage of 4 each index is : " + str (res)) |
The original list : [[3, 4, 5], [2, 4, 6], [3, 5, 4]] The percentage of 4 each index is : [0.0, 0.6666666666666666, 0.3333333333333333]
Method #3 : operator.countOf() + list comprehension
Python3
import operator as op # Python3 code to demonstrate # index percentage calculation of element # using zip() + operator.countOf() + list comprehension # initializing test list test_list = [[ 3 , 4 , 5 ], [ 2 , 4 , 6 ], [ 3 , 5 , 4 ]] # printing original list print ( "The original list : " + str (test_list)) # using zip() + operator.countOf() + list comprehension # index percentage calculation of element res = [op.countOf(sub, 4 ) / len (sub) for sub in zip ( * test_list)] # print result print ( "The percentage of 4 each index is : " + str (res)) |
The original list : [[3, 4, 5], [2, 4, 6], [3, 5, 4]] The percentage of 4 each index is : [0.0, 0.6666666666666666, 0.3333333333333333]
Time Complexity: O(N*N), where n is the length of the given string
Auxiliary Space: O(N)
Method 4 : using the built-in function map() and the sum() function:
Step-by-step approach:
- Define the test list.
- Use the zip() function to transpose the test list, i.e., group the first elements of all sub-lists, group the second elements of all sub-lists, and so on. This results in a list of tuples, where each tuple contains the elements at the same index from each sub-list.
- Use a list comprehension to iterate over the tuples obtained from the previous step. For each tuple, use the map() function to check if the value 4 is present in the tuple, which returns a list of True and False values. Then, use the sum() function to count the number of Trues in the list, i.e., the number of times 4 occurs at that index. Finally, divide the count by the length of the tuple to get the percentage.
- Return the resulting list of percentages.
- Print the original list and the list of percentages.
Python3
# Python3 code to demonstrate # index percentage calculation of element # using map() + sum() + list comprehension # initializing test list test_list = [[ 3 , 4 , 5 ], [ 2 , 4 , 6 ], [ 3 , 5 , 4 ]] # printing original list print ( "The original list : " + str (test_list)) # using map() + sum() + list comprehension # index percentage calculation of element res = [ sum ( map ( lambda x: x = = 4 , sub)) / len (sub) for sub in zip ( * test_list)] # print result print ( "The percentage of 4 each index is : " + str (res)) |
The original list : [[3, 4, 5], [2, 4, 6], [3, 5, 4]] The percentage of 4 each index is : [0.0, 0.6666666666666666, 0.3333333333333333]
Time complexity: O(n*m), where n is the number of sub-lists and m is the length of the longest sub-list.
Auxiliary space: O(n*m), because the zip() function creates a new list of tuples, and the list comprehension creates a new list of percentages.