在列表中查找数字的倍数

时间:2017-12-24 05:13:40

标签: python-3.x numpy scipy

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

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.0099等。 其中a = 100 + 1*1.0033a = 100 + 2*1.0033a = 100 + 3*1.0033

所以结果就是索引。

索引的结果:

[2, 4, 6]

[101.0033, 102.0066, 103.0099]

表示值。

1 个答案:

答案 0 :(得分: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]