簡體   English   中英

從num_dict返回值大於或等於min_cutoff的所有鍵(按設置)

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

參量

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))
def keys_get_cutoff(dict, min_value):

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

使用列表理解。 查看有關列表理解的更多詳細信息

例如

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']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM