以下是一个名为"remove_numbers_larger_than"的函数的示例代码,该函数接受一个混合字典作为参数,并删除字典中所有值大于给定阈值的数字。
def remove_numbers_larger_than(dictionary, threshold):
keys_to_remove = []
for key, value in dictionary.items():
if isinstance(value, (int, float)) and value > threshold:
keys_to_remove.append(key)
for key in keys_to_remove:
del dictionary[key]
return dictionary
使用示例:
my_dict = {
"a": 1,
"b": 2,
"c": 3.5,
"d": "hello",
"e": 10
}
threshold = 3
result = remove_numbers_larger_than(my_dict, threshold)
print(result)
输出:
{'a': 1, 'b': 2, 'd': 'hello'}
在上面的示例中,字典my_dict
包含了不同类型的值,包括整数、浮点数和字符串。通过调用remove_numbers_larger_than
函数,并传入字典和阈值3,函数会删除字典中所有大于3的数字。最后,函数返回删除后的字典{'a': 1, 'b': 2, 'd': 'hello'}
。