我想拥有一个列表功能,并根据敏感度给我类似[4,4.01,3.9]
或[71,69,72]
的数字
我尝试减去每个值,如果它小于灵敏度,则将这些数字添加到元组中,例如:
abs(4 - 4.01)= 0.01 => 0.01 < 0.5 => result += [(4,4.01)]
或
abs(4.01 - 3.9)= 0.11 => 0.11 < 0.5 => result += [(4.01,3.9)]
但是它的输出是
[(4, 4.1), (4, 3.9), (4.1, 4), (4.1, 3.9), (3.9, 4), (3.9, 4.1)]
我不想要这个,我想要[4.01,4,3.9]
的{{1}}
python
[4,4.01,3.9,5,10,45]
答案 0 :(得分:0)
您可以使用filter
函数来执行此操作,以获取满足您条件的所有这些值:
WORKDIR /usr/apps/
输出:
inlist = [4,4.01,3.9,5,10,45]
point_value = 4
threshold = 0.5
outlist = list(filter(lambda elem: round(abs(elem-point_value),2) <= threshold, inlist))
print(outlist)