e a function my_within_tolerance(A, a, tol) where the output is an array or list of the indices in A such that |A − a| < tol

Viewed 141

I am trying to write a function which has the name my_within_tolerance(A, a, tol). A can be float list or array while a and tol are float numbers. The function should return the elements in A which are |A − a| < tol. I am facing issues writing the function. Please help me....

I am new to python.

Here is what I have written:

import numpy as np
def my_within_tolerance(A, a, tol):
  A=np.asarray(A)
  if((abs(A-a)<tol).any()):
    return A

print(my_within_tolerance([0, 1, 2, 3], 1.5, 0.75))

Output should be:

[1, 2]
1 Answers

You could try this.

def my_within_tolerance(A,a,tol):
    x = [numbers for numbers in A if abs(numbers-a) < tol]
Related