简体   繁体   English

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

[英]Return all the keys (as set) from num_dict that have value greater than or equal to min_cutoff

Parameters 参量

num_dict: dictionary
  all values are numeric
min_cutoff: float

Compare with the num_dict values. num_dict值进行比较。 Return all keys where their values >= min_cutoff. 返回其值> = min_cutoff的所有键。

My dictionary is {'Denver': 200, 'Houston': 100, 'NOLA':50} 我的字典是{'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))
def keys_get_cutoff(dict, min_value):

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

Using list comprehension. 使用列表理解。 see more details about list comprehension 查看有关列表理解的更多详细信息

Ex. 例如

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: O / P:

['Denver', 'Houston']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 优化:从数组中返回大于(或等于)x的最小值 - Optimization: Return lowest value from array that is greater than (or equal to) `x` 如果未设置最小键,则返回与字典的最小值关联的键 - Return key associated with min value of a dict if min key is not in set 按值对字典排序,然后如果按键相等 - sort dict by value then if equal by keys 如果dict中的值相等,则返回键python - If values are equal in dict return the keys python 从字典中返回一组具有共同值的键对 - Return a set of pairs of keys from a dictionary that have a common value XGBoostError:参数 num_class 的值 0 应大于等于 1 - XGBoostError: value 0 for Parameter num_class should be greater equal to 1 如果 user_num2 大于 8,则为 user_num2 分配 5。打印“user_num2 小于或等于 8。” - Assign user_num2 with 5 if user_num2 is greater than 8. print "user_num2 is less than or equal to 8." 如何在python中为dict对象的所有键设置默认值? - How to set default value to all keys of a dict object in python? 如何从列表字典的值中获取所有键? - How to get all keys from a value for a dict of lists? 在没有一堆if语句的情况下从大于、小于和等于中获取布尔值? - Getting Boolean value from greater than, less than, and equal to without a bunch of if statements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM