Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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/5/spring-mvc/2.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_List_For Loop - Fatal编程技术网

Python 在一定范围内对列表进行四舍五入

Python 在一定范围内对列表进行四舍五入,python,list,for-loop,Python,List,For Loop,我有一个数字列表,我希望以某种方式转换为单位步骤形式 这就是我的界限 [367.3090551181102, 371.73179133858264, 376.1545275590551, 380.5772637795276, 385.0] 这是我掌握的数据 [367.3090551181102, 370, 370, 370, 370, 370, 370, 370, 369, 369, 369, 369, 369, 369, 369, 369, 369, 369, 369, 369, 368,

我有一个数字列表,我希望以某种方式转换为单位步骤形式

这就是我的界限

[367.3090551181102, 371.73179133858264, 376.1545275590551, 380.5772637795276, 385.0]
这是我掌握的数据

[367.3090551181102, 370, 370, 370, 370, 370, 370, 370, 369, 369, 369, 369, 369, 369, 369, 369, 369, 369, 369, 369, 368, 369, 368, 368, 368, 368, 368, 368, 368, 368, 369, 370, 367.3090551181102, 367.3090551181102, 367.3090551181102, 370, 368, 373, 369, 371, 368, 367.3090551181102, 367.3090551181102, 367.3090551181102, 369, 367.3090551181102, 372, 367.3090551181102, 368, 368, ...]
我想做的是基本上把数据中的每个数字向下或向上舍入到边界。数字介于下边界和上边界之间

假设数字是370,我想把它四舍五入到371.73。。。因为它在367.309之间。。。和371.73。。。或者数字是381,然后我想把它四舍五入到370.57

这是我的代码,但我有点困惑

def unit_step(data,boundaries):
    us = []
    for d in data:
        if(boundaries[0] < d < boundaries[1]):
            us.append(rounder(boundaries[0],boundaries[1],d))
        elif(boundaries[1] < d < boundaries[2]):
            us.append(rounder(boundaries[1],boundaries[2],d))
        elif(boundaries[2] < d < boundaries[3]):
            us.append(rounder(boundaries[2],boundaries[3],d))
        elif(boundaries[3] < d < boundaries[4]):
            us.append(rounder(boundaries[3],boundaries[4],d))
    return us
def装置步骤(数据、边界):
美国=[]
对于数据中的d:
如果(边界[0]
我在另一个for循环中尝试了for循环,但没有成功


与double for loop相比,我哪里错了,或者这是一种有用的方法?

您可以使用
min
的列表理解来检查
边界中最近的(较高或较低的)值,并相应地替换
数据中的值

>>> [min(boundaries, key=lambda i: abs(i - value)) for value in data]
[367.3090551181102, 371.73179133858264, 371.73179133858264, 371.73179133858264, 371.73179133858264, 371.73179133858264, 371.73179133858264, 371.73179133858264, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 371.73179133858264, 367.3090551181102, 367.3090551181102, 367.3090551181102, 371.73179133858264, 367.3090551181102, 371.73179133858264, 367.3090551181102, 371.73179133858264, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 371.73179133858264, 367.3090551181102, 367.3090551181102, 367.3090551181102]

您可以使用列表理解和
min
检查
边界中最近的(较高或较低的)值,并相应地替换
数据中的值

>>> [min(boundaries, key=lambda i: abs(i - value)) for value in data]
[367.3090551181102, 371.73179133858264, 371.73179133858264, 371.73179133858264, 371.73179133858264, 371.73179133858264, 371.73179133858264, 371.73179133858264, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 371.73179133858264, 367.3090551181102, 367.3090551181102, 367.3090551181102, 371.73179133858264, 367.3090551181102, 371.73179133858264, 367.3090551181102, 371.73179133858264, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 371.73179133858264, 367.3090551181102, 367.3090551181102, 367.3090551181102]

这回答了你的问题吗@MadyDaby不不是我要找的那个这个能回答你的问题吗@MadyDaby不是我要找的那个