Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.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_Dictionary_Rounding - Fatal编程技术网

在python中查找字典中的键

在python中查找字典中的键,python,dictionary,rounding,Python,Dictionary,Rounding,以下代码的目的是返回输入直径的最小管道坡度。如果直径输入(以米为单位)不在键值范围内,代码将返回错误 D_MINSLOPE = {100:60, 150:100, 225:300, 300:400, 375:550, 450:700, 525:750, 600:900, 675:1050, 750:1200, 825:1380, 900:1600, 1050:2000, 1200:2400, 1350:2800, 1500:3250, 1650:3700, 1800:4200} def min

以下代码的目的是返回输入直径的最小管道坡度。如果直径输入(以米为单位)不在键值范围内,代码将返回错误

D_MINSLOPE = {100:60, 150:100, 225:300, 300:400, 375:550, 450:700, 525:750, 600:900, 675:1050, 750:1200, 825:1380, 900:1600, 1050:2000, 1200:2400, 1350:2800, 1500:3250, 1650:3700, 1800:4200}

def minslope(DIA):
    DIA_mm = round(DIA * 1000)
    Slope = 1/D_MINSLOPE[DIA_mm]
    return Slope
目前,我将使用“try/except”“if/elif”来解决问题,以根据不同的基数(50、75、150)进行汇总

是否有一种更符合python的方法来舍入关键值


我注意到这个问题与数组类似。

IIUC,下面是相关的示例,您可以在字典中搜索最接近的
键。例如,如果要搜索
500

min(D_MINSLOPE,key=lambda x:abs(x-500))

将返回
525
,这是
字典
键中最接近的值

因此,您可以尝试将函数更改为:

def minslope(DIA):
    # convert Diameter (m) to Diameter (mm)
    DIA_mm = DIA * 1000        
    # get closest value
    DIA_cv = min(D_MINSLOPE, key=lambda x:abs(x-DIA_mm))   
    Slope = 1/D_MINSLOPE[DIA_cv]
    return Slope

与上述方法不完全相同,但这是我对这个问题的看法。我将键字典重新创建为间隔,如果
DIA
落在该间隔内,它将返回该值,而不是使用最接近的值

此外,您可能希望将1更改为1.0,以确保您的值返回的是浮点数而不是整数。(我想这就是你想要的)。

D_MINSLOPE = {100:60, 150:100, 225:300, 300:400, 375:550, 450:700, 525:750,
              600:900, 675:1050, 750:1200, 825:1380, 900:1600, 1050:2000,
              1200:2400, 1350:2800, 1500:3250, 1650:3700, 1800:4200}


def change_dict(dictionary):
    """
    Changes the dictionary to contain intervals as keys

    Returns:
        {(1651, 1800): 4200, (301, 375): 550,
         (1201, 1350): 2800, (451, 525): 750,
         (751, 825): 1380, (526, 600): 900,
         (1, 100): 60, (676, 750): 1200,
         (1051, 1200): 2400, (1351, 1500): 3250,
         (826, 900): 1600, (901, 1050): 2000,
         (376, 450): 700, (151, 225): 300,
         (101, 150): 100, (1501, 1650): 3700,
         (601, 675): 1050, (226, 300): 400}
    """
    new_dict = {}
    temp = [0] + sorted(D_MINSLOPE.keys())
    i = 0
    while i < len(D_MINSLOPE.keys()):
        key = temp[i+1]; value = D_MINSLOPE[key]
        new_key = (temp[i] + 1, temp[i+1])
        new_dict[new_key] = value
        i += 1
    return new_dict


def minslope(DIA):
    DIA_mm = round(DIA * 1000)
    D_MINSLOPE_2 = change_dict(D_MINSLOPE)    # Change dictionary keys

    # Find the key in which DIA_mm falls under
    key = None
    for i in D_MINSLOPE_2:
        if (i[0] <= DIA) and (i[1] >= DIA):
            key = i
            break

    # Change 1 to 1.0, as it needs to be a float number to output decimals
    # otherwise it would probably output 0 as an integer.
    return 1.0/D_MINSLOPE_2[key]    
D_MINSLOPE={100:60150:100225:300300:400375:550450:700525:750,
600:900, 675:1050, 750:1200, 825:1380, 900:1600, 1050:2000,
1200:2400, 1350:2800, 1500:3250, 1650:3700, 1800:4200}
def change_dict(字典):
"""
将字典更改为包含作为键的间隔
返回:
{(1651, 1800): 4200, (301, 375): 550,
(1201, 1350): 2800, (451, 525): 750,
(751, 825): 1380, (526, 600): 900,
(1, 100): 60, (676, 750): 1200,
(1051, 1200): 2400, (1351, 1500): 3250,
(826, 900): 1600, (901, 1050): 2000,
(376, 450): 700, (151, 225): 300,
(101, 150): 100, (1501, 1650): 3700,
(601, 675): 1050, (226, 300): 400}
"""
new_dict={}
temp=[0]+已排序(D_MINSLOPE.keys())
i=0
而我
如果直径不在列表中,是否需要最接近的值?不需要
.keys()不过在这一点上,您可能不应该使用字典。@juanpa.arrivillaga我不确定,您有什么建议?元组列表也可以(可能更快)。如果您迭代字典中的每个键来检索一个值,那么这将破坏整个目的。