Question: - When analysing data collected as part of a science experiment it may be desirable to remove the most extreme values before performing other calculations.


- When analysing data collected as part of a science experiment it may be desirable to remove the most extreme values before performing other calculations. Write a function that takes a list of values and a non-negative integer, n, as its parameters. The function should create a new copy of the list with the n largest elements and the n smallest elements removed. Then it should return the new copy of the list as the function's only result. The order the elements in the returned list does not have to match the order of the elements in the original list. Write a Python script that demonstrates your function. - Your function should read a list of numbers from the user and remove the two largest and two smallest values from it. Display the list with the outliers removed, followed by the original list. Your program should generate an appropriate error message if the user enters less than 4 values. Ref: The Python Workbook by Ben Stephenson (\#106) I def remove_outliers(nu_list, nu_outliers): \# The function assumes that there are enough elements in nu_list to be removed temp_lst = sorted(nu_list) \# Removing the Largest values for i in range(nu_outliers): temp_lst.pop() \# Removing the smallest values for i in range(nu_outliers): temp_lst.pop( 0) return temp_lst Nu_list =[1,4,9,2,5,8,3,19,1] nu_outliers =2 \# Checking if there are enough elements in the List to be removed if len(nu_list) 2 *nu_outliers: print(f'There are not enough elements in the list such that {2 nu_outliers } can be removed') elif len(nu_list) =2 nu_outliers: print ( list ()) else: print(remove_outliers(nu_list, nu_outliers)) [2,3,4,5,8] You are now asked to modify this script to implement the function that creates a new copy of the input data with the following two conditions: - The outliers should be replaced by None instead of being removed - All the data points should remain in their original locations. For example, in the test case shown with the script above, the output should be given as: [None, 4 , None, 2, 5, 8, 3, None, None] \# Your code should go here
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
