
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find mean of array after removing some elements in Python
Suppose we have array called nums, we have to find the mean of the remaining values after removing the smallest 5% and the largest 5% of the elements.
So, if the input is like nums = [2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,8], then the output will be 4.0 because after removing smallest and largest values, all are same, then the median is
To solve this, we will follow these steps −
sort the list nums
n := size of nums
per := quotient of (n*5/100)
l2 := subarray of nums from index per to (size of nums - per - 1)
x := average of all elements in l2
return x
Example (Python)
Let us see the following implementation to get better understanding −
def solve(nums): nums.sort() n = len(nums) per = int(n*5/100) l2 = nums[per:len(nums)-per] x = sum(l2)/len(l2) return x nums = [2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,8] print(solve(nums))
Input
[2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,8]
Output
4.0
- Related Articles
- Program to find string after removing consecutive duplicate characters in Python
- Program to find shortest string after removing different adjacent bits in Python
- Program to check maximum sum of all stacks after popping some elements from them in Python
- C++ program to find array after removal of left occurrences of duplicate elements
- Swift Program to get array elements after N elements
- Golang Program to get array elements after N elements
- C++ program to find array after inserting new elements where any two elements difference is in array
- Program to find min length of run-length encoding after removing at most k characters in Python
- Program to find minimum amplitude after deleting K elements in Python
- Removing empty array elements in PHP
- Swift Program to Find Mean of an Unsorted Array
- Adding and Removing Elements in Perl Array
- Program to find maximum score from removing stones in Python
- Find minimum possible size of array with given rules for removing elements in C++
- Find Mean of a List of Numpy Array in Python

Advertisements