如何在Python中扩展浮点列表?

如何在Python中扩展浮点列表?,python,algorithm,scaling,Python,Algorithm,Scaling,我在Python中工作,有一个一天的小时值列表。为了简单起见,假设一天只有10个小时 [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0] 我想将其围绕中心点延伸到150%,最终得到: [0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0] 请注意,这只是一个示例,我还需要按给定小时内留下分数的数量来拉伸。例如,拉伸到125%将得到: [0.0, 0.0, 0.5, 1.0, 1.0, 1.0

我在Python中工作,有一个一天的小时值列表。为了简单起见,假设一天只有10个小时

 [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0]
我想将其围绕中心点延伸到150%,最终得到:

 [0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0]
请注意,这只是一个示例,我还需要按给定小时内留下分数的数量来拉伸。例如,拉伸到125%将得到:

 [0.0, 0.0, 0.5, 1.0, 1.0, 1.0, 1.0, 0.5, 0.0, 0.0]
我处理分数金额的第一个想法是使用
np将列表乘以10。重复
,应用一些方法将值延伸到中点附近,最后将列表分成10块,并取每小时的平均值


我的主要问题是“拉伸”部分,但如果答案也能解决第二部分的问题,那就更好了。

我想,你需要这样的东西:

def stretch(xs, coef):
  # compute new distibution
  oldDist = sum(hours[:len(hours)/2])
  newDist = oldDist * coef

  # generate new list
  def f(x):
    if newDist - x < 0:
      return 0.0
    return min(1.0, newDist - x)

  t = [f(x) for x in range(len(xs)/2)]
  res = list(reversed(t))
  res.extend(t)
  return res
def拉伸(xs,coef):
#计算新分布
oldDist=总和(小时[:len(小时)/2])
newDist=oldDist*coef
#生成新列表
def f(x):
如果newDist-x<0:
返回0.0
返回最小值(1.0,newDist-x)
t=[f(x)表示范围内的x(len(xs)/2)]
res=列表(反向(t))
决议(t)
返回res

但要注意奇数小时。

如果我查看预期输出,算法如下所示:

  • 从数字列表开始,值>0.0表示工作时间
  • 把这些时间加起来
  • 计算需要多少额外的小时
  • 分开那些 通过预加或追加在序列两端额外的小时数 每个“末端”有一半
因此:

作为输出提供:

Before expansion: [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0]
After  expansion: [0.0, 0.0, 0.6, 1.0, 1.0, 1.0, 1.0, 0.6, 0.0, 0.0]

这就是我最后要做的。这有点难看,因为它需要处理小于100%的拉伸系数

def stretch(xs, coef, centre):
    """Scale a list by a coefficient around a point in the list.

    Parameters
    ----------
    xs : list
        Input values.
    coef : float
        Coefficient to scale by.
    centre : int
        Position in the list to use as a centre point.

    Returns
    -------
    list

    """
    grain = 100    
    stretched_array = np.repeat(xs, grain * coef)

    if coef < 1:
        # pad start and end
        total_pad_len = grain * len(xs) - len(stretched_array)
        centre_pos = float(centre) / len(xs)
        start_pad_len = centre_pos * total_pad_len
        end_pad_len = (1 - centre_pos) * total_pad_len
        start_pad = [stretched_array[0]] * int(start_pad_len)
        end_pad = [stretched_array[-1]] * int(end_pad_len)
        stretched_array = np.array(start_pad + list(stretched_array) + end_pad)
    else:
        pivot_point = (len(xs) - centre) * grain * coef
        first = int(pivot_point - (len(xs) * grain)/2)
        last = first + len(xs) * grain
        stretched_array = stretched_array[first:last]

    return [round(chunk.mean(), 2) for chunk in chunks(stretched_array, grain)]


def chunks(iterable, n):
    """
    Yield successive n-sized chunks from iterable.
    Source: http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python#answer-312464

    """
    for i in xrange(0, len(iterable), n):
        yield iterable[i:i + n]
def拉伸(xs、coef、中心):
“”“通过围绕列表中某个点的系数缩放列表。
参数
----------
xs:list
输入值。
系数:浮动
比例系数。
中心:国际
列表中用作中心点的位置。
退换商品
-------
列表
"""
谷物=100
拉伸数组=np.重复(xs,粒度*coef)
如果coef<1:
#焊盘开始和结束
总长度=粒度*长度(xs)-长度(拉伸阵列)
中心位置=浮动(中心)/透镜(xs)
开始\u pad_ulen=中心位置*总\u pad\u len
端部垫长=(1-中心位置)*总垫长
起始焊盘=[拉伸焊盘阵列[0]]*int(起始焊盘长度)
end_pad=[拉伸_数组[-1]]*int(end_pad_len)
拉伸数组=np.数组(开始数组+列表(拉伸数组)+结束数组)
其他:
枢轴点=(透镜(xs)-中心)*晶粒*coef
第一个=整数(枢轴点-(透镜(X)*晶粒)/2)
最后一个=第一个+长度(xs)*晶粒
拉伸数组=拉伸数组[第一:最后]
return[chunk-in-chunk(拉伸数组,grain)的round(chunk.mean(),2)]
def块(iterable,n):
"""
从iterable中生成连续的n大小的块。
资料来源:http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python#answer-312464
"""
对于x范围内的i(0,len(iterable),n):
收益率可比[i:i+n]

数组在初始状态下是否总是只填充1.0和0.0?您的列表是否总是这样:一堆零,一些数字,一堆零?元素的最大值是否为1.0?数字是否总是围绕中间对称?不,它们可以是零或任何(正)值,不一定围绕中间,而是围绕某个相当中心的点(实际上是工作日中间的下午1点,但我希望能够改变这一点)。那么,你应该延伸到什么百分比,如果要将一个元素展开为整个列表?请更好地解释你们的算法你们可以做一些变通,但从问题的上下文来看,像熊猫这样的软件包可能是你们最好的选择。您可以定义随时间变化的测量值,然后使用其功能填充和查询不同的范围。
def stretch(xs, coef, centre):
    """Scale a list by a coefficient around a point in the list.

    Parameters
    ----------
    xs : list
        Input values.
    coef : float
        Coefficient to scale by.
    centre : int
        Position in the list to use as a centre point.

    Returns
    -------
    list

    """
    grain = 100    
    stretched_array = np.repeat(xs, grain * coef)

    if coef < 1:
        # pad start and end
        total_pad_len = grain * len(xs) - len(stretched_array)
        centre_pos = float(centre) / len(xs)
        start_pad_len = centre_pos * total_pad_len
        end_pad_len = (1 - centre_pos) * total_pad_len
        start_pad = [stretched_array[0]] * int(start_pad_len)
        end_pad = [stretched_array[-1]] * int(end_pad_len)
        stretched_array = np.array(start_pad + list(stretched_array) + end_pad)
    else:
        pivot_point = (len(xs) - centre) * grain * coef
        first = int(pivot_point - (len(xs) * grain)/2)
        last = first + len(xs) * grain
        stretched_array = stretched_array[first:last]

    return [round(chunk.mean(), 2) for chunk in chunks(stretched_array, grain)]


def chunks(iterable, n):
    """
    Yield successive n-sized chunks from iterable.
    Source: http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python#answer-312464

    """
    for i in xrange(0, len(iterable), n):
        yield iterable[i:i + n]