Python – Average of digit greater than K
Given elements list, extract elements whose average of digit is greater than K.
Input : test_list = [633, 719, 8382, 119, 327], K = 5
Output : [719, 8382]
Explanation : (7 + 1 + 9) / 3 = 5.6 and (8 + 3 + 8 + 2) / 4 = 5.2 , both of which are greater than 5, hence returned.Input : test_list = [633, 719, 8382, 96], K = 5
Output : [719, 8382, 96]
Explanation : All the elements are displayed in output whose digit average is greater than 5.
Method #1 : Using list comprehension + sum() + len()
In this, we compute digits sum using sum(), and then divide the sum by element length to get average, and add in result if it’s greater than K.
Python3
# Python3 code to demonstrate working of # Average digit greater than K # Using sum() + len() + list comprehension # initializing list test_list = [ 633 , 719 , 8382 , 119 , 327 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 5 # getting average and checking if greater than K res = [sub for sub in test_list if sum ( [ int (ele) for ele in str (sub)]) / len ( str (sub)) > = K] # printing result print ( "Filtered List : " + str (res)) |
Output:
The original list is : [633, 719, 8382, 119, 327] Filtered List : [719, 8382]
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. list comprehension + sum() + len() performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #2 : Using sum() + len() + filter() + lambda
Here, filtering operation is performed using filter() and lambda, rest all operations are handled as per above method.
Python3
# Python3 code to demonstrate working of # Average digit greater than K # Using sum() + len() + filter() + lambda # initializing list test_list = [ 633 , 719 , 8382 , 119 , 327 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 5 # getting average and checking if greater than K # using filter() and lambda to filter res = list ( filter ( lambda sub: sum ( [ int (ele) for ele in str (sub)]) / len ( str (sub)) > = K, test_list)) # printing result print ( "Filtered List : " + str (res)) |
Output:
The original list is : [633, 719, 8382, 119, 327] Filtered List : [719, 8382]
Method #3 : Using mean() from statistics module
Convert each list element to string and then each string to integer list, computing average using mean() method of statistics . Later we fill those elements whose average is greater than K
Python3
# Python3 code to demonstrate working of # Average digit greater than K from statistics import mean # initializing list test_list = [ 633 , 719 , 8382 , 119 , 327 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 5 x = [] for i in range ( 0 , len (test_list)): x.append( str (test_list[i])) y = [] for i in range ( 0 , len (x)): a = mean( list ( map ( int ,x[i]))) y.append(a) res = [] for i in range ( 0 , len (y)): #checking if greater than K if (y[i]>K): res.append(test_list[i]) # printing result print ( "Filtered List : " + str (res)) |
The original list is : [633, 719, 8382, 119, 327] Filtered List : [719, 8382]
Method #4: Using for loop
Python3
# Python3 code to demonstrate working of # Average digit greater than K # Using sum() + len() + list comprehension # initializing list test_list = [ 633 , 719 , 8382 , 119 , 327 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 5 # getting average and checking if greater than K res = [sub for sub in test_list if sum ( [ int (ele) for ele in str (sub)]) / len ( str (sub)) > = K] # printing result print ( "Filtered List : " + str (res)) #This code is contributed by Vinay Pinjala. |
The original list is : [633, 719, 8382, 119, 327] Filtered List : [719, 8382]
Time complexity: O(n)
space complexity: O(n)
Method #5: Using NumPy
Python3
import numpy as np # initializing list test_list = [ 633 , 719 , 8382 , 119 , 327 ] # printing original list print ( "The original list is: " , test_list) # initializing K K = 5 # converting each element of the list to a list of digits list_of_lists = [ list ( map ( int , str (x))) for x in test_list] # getting average and checking if greater than K res = [test_list[i] for i in range ( len (test_list)) if np.(list_of_lists[i]) > = K] # printing result print ( "Filtered List: " , res) |
Output:
The original list is: [633, 719, 8382, 119, 327] Filtered List: [719, 8382]
Time complexity: O(n)
Auxiliary Space: O(n)
Method 6: Using a generator expression and reduce() function
We can use a generator expression to generate the digits of each number and calculate their sum and count using the reduce() function with a lambda function. Then, we can use another generator expression to generate the average of digits for each number and filter out the numbers whose average digit is less than K. Finally, we can use list() function to convert the filter object into a list.
Python3
from functools import reduce # initializing list test_list = [ 633 , 719 , 8382 , 119 , 327 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 5 # getting average and checking if greater than K res = [num for num in test_list if (( reduce ( lambda x, y: int (x) + int (y), str (num)) / len ( str (num))) > = K)] # printing result print ( "Filtered List : " + str (res)) |
The original list is : [633, 719, 8382, 119, 327] Filtered List : [719, 8382]
Time Complexity: O(N), where N is the number of elements in the list.
Auxiliary Space: O(1)
Contact Us