简体   繁体   中英

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. Return all keys where their values >= min_cutoff.

My dictionary is {'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:

['Denver', 'Houston']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM