从num_dict返回值大于或等于min_cutoff的所有键(按设置)

时间:2019-07-18 22:11:54

标签: python dictionary

参数

num_dict: dictionary
  all values are numeric
min_cutoff: float

num_dict值进行比较。返回其值> = min_cutoff的所有键。

我的字典是{'Denver': 200, 'Houston': 100, 'NOLA':50}

def keys_get_cutoff(num_dict, min_cutoff):
    for k, v in num_dict.items():
        if v >= min_cutoff:
            print(keys_get_cutoff(num_dict, min_cutoff))

2 个答案:

答案 0 :(得分:0)

def keys_get_cutoff(dict, min_value):

    return [key for key in dict.keys() if dict[key] >= min_value]

答案 1 :(得分:0)

使用列表理解。 see more details about list comprehension

例如。

num_dict =  {'Denver': 200, 'Houston': 100, 'NOLA':50}
min_cutoff = 51
num_dict_keys = [k for k, v in num_dict.items() if v >= min_cutoff ]
print(num_dict_keys)

O / P:

['Denver', 'Houston']