Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.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使用插值将列表中元素的大小增加n个_Python_Python 3.x_List_Matplotlib_Interpolation - Fatal编程技术网

Python使用插值将列表中元素的大小增加n个

Python使用插值将列表中元素的大小增加n个,python,python-3.x,list,matplotlib,interpolation,Python,Python 3.x,List,Matplotlib,Interpolation,我有几个数字的清单。我想使用插值将列表的大小再增加n个数字。 我的现有代码和输出: R_data = [10, 40, 40, 40, 15] R_op = [random.choice(R_data) for i in range(100)] plt.plot(R_op,'-s') plt.show() 现有数据: 现有产出: 预期产出: 听起来您想要一个线性关系,并在现有值之间插入新值。我将编写一个函数,在两个现有值之间的行上生成一些数字N值,然后使用该值和原始R\u数据中的所有间隔组

我有几个数字的清单。我想使用插值将列表的大小再增加n个数字。 我的现有代码和输出:

R_data = [10, 40, 40, 40, 15]
R_op = [random.choice(R_data) for i in range(100)]
plt.plot(R_op,'-s')
plt.show()
现有数据:

现有产出:

预期产出:

听起来您想要一个线性关系,并在现有值之间插入新值。我将编写一个函数,在两个现有值之间的行上生成一些数字N值,然后使用该值和原始
R\u数据中的所有间隔组成新列表。像这样的事情应该可以做到:

def interpolate_pts(start, end, num_pts):
    """ Returns a list of num_pts float values evenly spaced on the line between start and end """
    interval = (float(end)-float(start)) / float(num_pts + 1)
    new_pts = [0] * num_pts
    for index in range(num_pts):
        new_pts[index] = float(start) + (index+1)*interval
    return new_pts

R_data = [10.0, 40.0, 40.0, 40.0, 15.0]
num_pts_to_add_between_data = 4
new_data = []
for index in range(len(R_data)-1):
    first_num = R_data[index]
    second_num = R_data[index+1]
    new_data.append(first_num)  # Put the 1st value in the output list
    new_pts = interpolate_pts(first_num, second_num, num_pts_to_add_between_data)
    new_data.extend(new_pts)
new_data.append(R_data[-1]). # Add the final value to the output list

>>> print(new_data)
[10.0, 16.0, 22.0, 28.0, 34.0, 40.0, 40.0, 40.0, 40.0, 40.0, 40.0, 40.0, 40.0, 40.0, 40.0, 40.0, 35.0, 30.0, 25.0, 20.0, 15.0]

希望对你有帮助,编码快乐

我看这里没有问题。我不知道它是否满足现有的关系,但您确实是从原始列表中选择了数字。@Sam抱歉这个错误。我重写了我的问题。我只是想要插值数据。