在列表中查找数字的倍数

问题描述 投票:1回答:1

我有一个列表,我想在一定的容差范围内找到该数字的所有倍数,并获得它们的索引:

def GetPPMError(t, m):
    """
    calculate theoretical (t) and measured (m) ppm
    """
    return (((t - m) / t) * 1e6)

multiple = n*1.0033
a = 100 
b = [100, 101, 101.0033,102, 102.0066,102.123,103.0099]

results = [p for p in b if abs(GetPPMError(a,b)) < 10]

所以我想找到所有的倍数,如102.0066103.0099a = 100 + 1*1.0033a = 100 + 2*1.0033a = 100 + 3*1.0033

结果就是索引。

指数结果:

[2, 4, 6]

和:

[101.0033, 102.0066, 103.0099]

对于价值观。

python-3.x numpy scipy
1个回答
0
投票

这适用于您的数据:

multiple = 1.0033
a = 100 
digits = 6

res = []
res_index = []
for n, x in enumerate(b):
    diff = round((x - a) / multiple, digits)
    if diff != 0 and diff == int(diff):
        res.append(x)
        res_index.append(n)
print(res)
print(res_index)

输出:

[101.0033, 102.0066, 103.0099]
[2, 4, 6]
© www.soinside.com 2019 - 2024. All rights reserved.