[英]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.