Python – 查找字典中具有特定后缀的键
键的后缀是在单词末尾提到的一组字母。在这个问题陈述中,我们需要创建特定的变量,将后缀键设置为使用某些特定条件过滤结果。在 Python 中,我们有一些内置函数,例如 endswith()、filter()、lambda、append() 和 items(),将用于查找字典中具有特定后缀的键。
让我们来看一个例子。
给定的字典:
my_dict = {'compiler': 100, 'interpreter': 200, 'cooperative': 300, 'cloudbox': 400, 'database': 500}
后缀键设置为“er”,
因此,最终输出变为 {'compiler': 100, 'interpreter': 20}
语法
以下语法在示例中使用
endswith()
这是 Python 中预定义的方法,如果字符串以给定值结尾,则返回 true,否则返回 false。
filter()
当我们需要根据特定条件过滤项目时,将应用 filter() 方法。简单来说,它允许迭代那些被提取以满足条件的元素。
lambda
lambda 函数提供了一种使用 lambda 关键字声明简短匿名函数的快捷方式。lambda 函数只有一个表达式。
append()
append() 是 Python 中一个内置函数,它将元素插入列表的末尾。
items()
这是一个内置方法,可用于返回视图对象。该对象包含键值对。
使用列表推导式
在下面的示例中,程序使用列表推导式技术,其中变量 key 迭代输入字典,并使用 if 语句设置具有特定后缀的键的条件,借助内置函数 endwith() 显示结果。
示例
def key_suffix(dict, suffix): return [key for key in dict if key.endswith(suffix)] # Create the dictionary my_dict = {"dropbox": 1, "box": 2, "chat": 3, "mail": 4} sfx = "ox" # Calling function res = key_suffix(my_dict, sfx) # Display the result print("Keys with suffix","[", sfx, "]", ":\n", res)
输出
Keys with suffix [ ox ] : ['dropbox', 'box']
使用 filter() 函数
在下面的示例中,程序使用名为 key_suffix 的递归函数,该函数接受两个参数 - dict 和 suffix 来接收输入字典和后缀值。然后使用一些内置函数 - filter()[过滤给定后缀元素以获得结果]、lambda[使用内置函数 endswith() 计算后缀操作],最后它将使用内置函数 list() 将后缀值作为列表的形式获取结果。
示例
def key_suffix(dict, suffix): return list(filter(lambda key: key.endswith(suffix), dict)) # Create the list my_dict = {"Evergreen": 1, "Beautiful": 2, "Fruitful": 3, "Golden": 4, "Brass": 5} sfx = "ful" # Calling function res = key_suffix(my_dict, sfx) # Display the display print("Keys with suffix","[", sfx, "]", ":\n", res)
输出
Keys with suffix [ ful ] : ['Beautiful', 'Fruitful']
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
使用循环和条件语句
在下面的示例中,程序使用递归函数设置空列表,该列表在函数返回期间以列表的形式存储结果。然后使用 for 循环迭代程序的输入字典,并使用 if 语句使用内置函数 endswith() 和 append() 设置具有特定后缀的键的条件。接下来,它将实现函数返回以获取特定后缀作为结果。
示例
def key_suffix(dict, suffix): keys_with_suffix = [] # Set the condition for operation and iteration of the specific suffix for key in dict: if key.endswith(suffix): keys_with_suffix.append(key) return keys_with_suffix # Create the dictionary my_dict = {"apple": 1, "banana": 2, "cherry": 3, "orange": 4, "Grapes": 5} sfx = "y" # Calling function res = key_suffix(my_dict, sfx) print("Keys with suffix","[", sfx, "]", ":\n", res)
输出
Keys with suffix [ y ] : ['cherry']
字典推导式
在下面的示例中,我们将使用字典推导式,其中第一个参数设置为键值对。使用 items(),它将视图对象检查到字典中,并应用 if 语句使用内置函数 endswith() 设置键的条件,该函数指定具有特定后缀的键并生成结果。
示例
def key_suffix(dict, suffix): return {key: value for key, value in dict.items() if key.endswith(suffix)} # Create the dictionary my_dict = {"Anonyms": 1, "Synonyms": 2, "Metaphor": 3, "Irony": 4} sfx = "ms" # Calling function res = key_suffix(my_dict, sfx) print("Keys with suffix","[", sfx, "]", ":\n", res)
输出
Keys with suffix [ ms ] : {'Anonyms': 1, 'Synonyms': 2}
结论
我们知道字典中具有特定后缀的特定键是以字符串形式表示的键,并以整数形式指定值。这里使用了一些方法,例如字典推导式、列表推导式、filter() 和条件语句来解决这个问题陈述。这种类型的程序通常用于各种应用程序,例如数据分析、数据过滤、搜索算法上的逻辑构建等。