Python – Average of digit greater than K
Last Updated :
30 Apr, 2023
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
test_list = [ 633 , 719 , 8382 , 119 , 327 ]
print ( "The original list is : " + str (test_list))
K = 5
res = [sub for sub in test_list if sum (
[ int (ele) for ele in str (sub)]) / len ( str (sub)) > = K]
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
test_list = [ 633 , 719 , 8382 , 119 , 327 ]
print ( "The original list is : " + str (test_list))
K = 5
res = list ( filter ( lambda sub: sum (
[ int (ele) for ele in str (sub)]) / len ( str (sub)) > = K, test_list))
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
from statistics import mean
test_list = [ 633 , 719 , 8382 , 119 , 327 ]
print ( "The original list is : " + str (test_list))
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)):
if (y[i]>K):
res.append(test_list[i])
print ( "Filtered List : " + str (res))
|
Output
The original list is : [633, 719, 8382, 119, 327]
Filtered List : [719, 8382]
Method #4: Using for loop
Python3
test_list = [ 633 , 719 , 8382 , 119 , 327 ]
print ( "The original list is : " + str (test_list))
K = 5
res = [sub for sub in test_list if sum (
[ int (ele) for ele in str (sub)]) / len ( str (sub)) > = K]
print ( "Filtered List : " + str (res))
|
Output
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
test_list = [ 633 , 719 , 8382 , 119 , 327 ]
print ( "The original list is: " , test_list)
K = 5
list_of_lists = [ list ( map ( int , str (x))) for x in test_list]
res = [test_list[i] for i in range ( len (test_list)) if np.(list_of_lists[i]) > = K]
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
test_list = [ 633 , 719 , 8382 , 119 , 327 ]
print ( "The original list is : " + str (test_list))
K = 5
res = [num for num in test_list if (( reduce ( lambda x, y: int (x) + int (y), str (num)) / len ( str (num))) > = K)]
print ( "Filtered List : " + str (res))
|
Output
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)
Share your thoughts in the comments
Please Login to comment...