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)
Similar Reads
Python | Get the Index of first element greater than K
Python list operations are always desired to have shorthands as they are used in many places in development. Hence having knowledge of them always remains quite useful. Let's deals with finding one such utility of having index of first element greater than K by one-liner. There are various ways in w
6 min read
Sum the Digits of a Given Number - Python
The task of summing the digits of a given number in Python involves extracting each digit and computing their total . For example, given the number 12345, the sum of its digits is 1 + 2 + 3 + 4 + 5 = 15. Using modulo (%)This method efficiently extracts each digit using the modulus (%) and integer di
2 min read
Average of Float Numbers - Python
The task of calculating the average of float numbers in Python involves summing all the numbers in a given list and dividing the total by the number of elements in the list. For example, given a list of float numbers a = [6.1, 7.2, 3.3, 9.4, 10.6, 15.7], the goal is to compute the sum of the numbers
3 min read
Python | Indices of numbers greater than K
Many times we might have problem in which we need to find indices rather than the actual numbers and more often, the result is conditioned. First approach coming to mind can be a simple index function and get indices greater than particular number, but this approach fails in case of duplicate number
4 min read
Python - Get Random Range Average
Given range and Size of elements, extract random numbers within a range, and perform average of it. Input : N = 3, strt_num = 10, end_num = 15 Output : 13.58 Explanation : Random elements extracted between 10 and 15, averaging out to 13.58. Input : N = 2, strt_num = 13, end_num = 18 Output : 15.82 E
5 min read
Find Sum and Average of List in Python
Our goal is to find sum and average of List in Python. The simplest way to do is by using a built-in method sum() and len(). For example, list of numbers is, [10, 20, 30, 40, 50] the sum is the total of all these numbers and the average is the sum divided by the number of elements in the list. Using
2 min read
Python - Nth smallest Greater than K
This article offers various approaches to solve the problem of finding Nth smallest number in python list greater than a specific element K in python list. This basically provides all the approaches from naive to one-liners so that they can be used in programming whenever required. Method 1 : Naive
8 min read
Python - Convert Float to digit list
We are given a floating-point number and our task is to convert it into a list of its individual digits, ignoring the decimal point. For example, if the input is 45.67, the output should be [4, 5, 6, 7]. Using string manipulationIn this method, the number is converted to a string and then each chara
4 min read
Python - Average digits count in a List
Given a list of elements extract the average digit count in List. Input : test_list = [34, 2345, 23, 456, 2, 23, 456787] Output : 2.857142857142857 Explanation : Average of all digit count. [2+4+2+3+1+2+6 = 20, 20/7 = 2.857142857142857] Input : test_list = [34, 1, 456]Output : 2.0 Explanation : Aver
4 min read
Python - Rear elements Average in List
Sometimes, while working with data, we can have a problem in which we need to perform the mean of all the rear elements that come after K. This can be an application in Mathematics and Data Science domain. Let us discuss certain ways in which this task can be performed. Method #1 : Using sum() + li
7 min read