Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python系列算法_Python_Algorithm_List_Linear Interpolation - Fatal编程技术网

Python系列算法

Python系列算法,python,algorithm,list,linear-interpolation,Python,Algorithm,List,Linear Interpolation,我有这样一个列表: numbers = [12, None, None, 17, 20, None, 28] P>什么是最好的路径,以便用适当的数字填充任何值,以便无值在定义的值之间得到数字。< /P> 例如: numbers = [12, **14**, **16**, 17, 20, **24**, 28] 我起初没有看到被替换索引需要介于两者之间的要求 def interpolate(x): while None in x: indexa = x.index(

我有这样一个列表:

numbers = [12, None, None, 17, 20, None, 28]
<> P>什么是最好的路径,以便用适当的数字填充任何值,以便无值在定义的值之间得到数字。< /P> 例如:

numbers = [12, **14**, **16**, 17, 20, **24**, 28]

我起初没有看到被替换索引需要介于两者之间的要求

def interpolate(x):
    while None in x:
        indexa = x.index(None)
        indexb = indexa + 1
        while (x[indexb]) is None:
            indexb = indexb + 1
        indexb = indexb - 1
        index = int((indexa + indexb) / 2)
        x[index] = y(index, x)
    return x


def y(j, x):
    a, b, j2 = 0.0, 0.0, j
    while x[j2] is None and j2 > -1:
        j2 = j2 - 1
    a = x[j2]
    while x[j] is None:
        j = j + 1
    b = x[j]
    return int((a + b) / 2)

print(interpolate([12, None, None, 17, 20, None, 28]))
print(interpolate([12, None, None, None, 17, 20, None, 28]))
print(interpolate([12, None, None, None, None, 17]))
nones = lambda i: [None for nothing in range(i)]
for i in range(10):
    print(interpolate([0] + nones(i) + [10]))
输出:

[12, 14, 15, 17, 20, 24, 28]
[12, 13, 14, 15, 17, 20, 24, 28]
[12, 13, 14, 15, 16, 17]
[0, 10]
[0, 5, 10]
[0, 5, 7, 10]
[0, 2, 5, 7, 10]
[0, 2, 5, 7, 8, 10]
[0, 2, 3, 5, 7, 8, 10]
[0, 2, 3, 5, 6, 7, 8, 10]
[0, 1, 2, 3, 5, 6, 7, 8, 10]
[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

动态插值以创建线性插值:

def interpolate(l):
    last = None
    count = 0
    for num in l:
        if num is None:
            # count the number of gaps
            count += 1
            continue
        if count:
            # fill a gap based on the last seen value and the current
            step = (num - last) / float(count + 1)
            for i in xrange(count):
                yield last + int(round((1 + i) * step))
            count = 0
        last = num
        yield num
演示:


你要找的词是“插值”。谷歌搜索。数字是如何选择的重要吗?@SimeonVisser,它们需要介于已知值之间,最好是均匀分布。当你说介于两者之间时,你的意思是非包容性的吗?也就是说,
12,None,None,17
不应该是
12,12,12,17
?@ZWiki如果没有找到其他非排他值,它可以是包含的。您在任何地方插入
4
。您的输出是
[1,4,2,4,3]
,与预期的输出相差甚远。@MartijnPieters更好吗?您的变量和函数名也是。。相当不透明。你应该试着想出比单个字母更好的名字。我在python3和python2上都没有这个错误。我得到了样本的
[12,14,14,17,20,24,28]
,以及输入的
[1,1,2,5,8,8,10,15,20]
。啊,我现在明白了。我有一个与另一个测试不同的
x
;这里假设
x
是一个全局变量。当我明确地将
x
设置为示例时,它就起作用了。从来都不是个好主意。
>>> list(interpolate([12, None, None, 17, 20, None, 28]))
[12, 14, 15, 17, 20, 24, 28]