我有一组从文件中读取的非唯一实数。
所有这些数字都是从线性空间生成的,也就是说,数字之间的差异始终是固定值的倍数," step"或"网格大小"线性空间,可以这么说。
每个现有值都会在文件中多次出现。
我的目标是找到值的间距,以便我可以将每个(唯一)值放在数组中并使用索引访问其值。
答案 0 :(得分:0)
您正在寻找这些数字的最大公约数。这是在Python中:
def gcd( a, b ):
"greatest common divisor"
while True:
c = a % b
if c < 1e-5:
return b
a, b = b, c
def gcdset( a_set ):
"use the pairwise gcd to find gcd of a set"
x = a_set.pop()
total = x
for u in a_set:
x = gcd( u, x )
# the following step is optional,
# some sort of stabilization just for improved accuracy
total = total + u
x = total / round(total/x)
return x
# the list where we want to find the gcd
inputlist = [2239.864226650253, 1250.4096410911607, 1590.1948696485413,
810.0479848807954, 2177.343744595695, 54.3656365691809, 2033.2748076873656,
2074.049035114251, 108.7312731383618, 2188.216871909531]
# we turn it into a set to get rid of duplicates
aset = set(inputlist)
print(gcdset( aset ))
如果您没有Python,可以在此处使用此代码:http://ideone.com/N9xDWA