【问题标题】: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| < tole 一个在容差(AS, a, tool) 内的函数,其中输出是 A 中索引的数组或列表,使得 |A - a| <托尔
【发布时间】:2021-07-14 05:41:06
【问题描述】:

我正在尝试编写一个名为my_within_tolerance(A, a, tol) 的函数。 A 可以是浮点数列表或数组,而 a 和 tol 是浮点数。该函数应返回 A 中的元素 |A − a|

我是 python 新手。

这是我写的:

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))

输出应该是:

[1, 2]

【问题讨论】:

    标签: python arrays python-3.x list function


    【解决方案1】:

    你可以试试这个。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多