Python – 从字符串列表中查找字典键
字典是 Python 中著名的数据类型之一,它由键值对组成。字符串列表是一组元素,这些元素用单引号或双引号表示。在 Python 中,有一些内置函数,例如 keys()、set()、intersection 和 append(),可用于查找字符串列表中存在的字典键。
让我们来看一个例子。
有两个可用的集合:
给定的字典:{'T': 1, 'M': 2, 'E': 3, 'C': 4}
给定的列表:[T, A, B, P, E, L]
因此,最终结果为 [T, E]。
语法
以下语法在示例中使用
keys()
keys() 是 Python 中的内置函数,它返回字典中所有可用键的列表。
set()
set() 是 Python 中的内置函数,可用于将多个元素存储到单个变量中。
intersection()
Python 的内置函数 intersection() 查找两个不同集合中的共同匹配项。
append()
append() 是 Python 中的内置函数,它将元素插入列表的末尾。
使用列表推导式
在下面的示例中,程序从名为 dict_keys() 的递归函数开始,该函数接受两个参数 d 和 s,以从给定的输入字典和字符串(即 dict1 和 stng)中获取值。接下来,它将使用列表推导式技术,首先将键设置为变量初始化。使用此变量,循环遍历字典以使用 if 语句检查条件,即字符串是否在给定字典中可用,并返回结果。
示例
def dict_keys(d, s):
return [key for key in d if key in s]
# The input dictionary and string list
dict1 = {'Tablets': 1, 'Mobiles': 2, 'Earbuds': 3, 'Computer': 4}
stng = ['Mouse', 'Hard Drive', 'Pen Drive', 'Earbuds', 'Tablets']
# Calling function
res = dict_keys(dict1, stng)
print("The result of dictionary keys present in a strings list:\n", res)
输出
The result of dictionary keys present in a strings list: ['Tablets', 'Earbuds']
使用 set() 和 intersection() 函数
在下面的示例中,名为 dict_keys() 的递归函数接受两个参数 d 和 s,以查找列表中存在的字典键。接下来,它将使用函数返回设置一些内置函数,例如 d.keys(),它显示字典的视图对象,而 intersection(s) 查找两个不同集合之间的共同匹配项。最后,这两个函数都设置为内置函数 list() 的参数,该函数将以列表的形式返回结果。
示例
def dict_keys(d, s):
return list(set(d.keys()).intersection(s))
# Create the dictionary and string list
dictionary = {'Television': 1, 'Projector': 2, 'White Board': 3}
strings = ['Projector', 'Box', 'Remote']
# Calling function
res = dict_keys(dictionary, strings)
print("The result of dictionary keys present in a strings list:\n", res)
输出
The result of dictionary keys present in a strings list: ['Projector']
使用简单的 for 循环
在下面的示例中,函数 dict_keys() 中将有两个参数 d_list 和 s_list,它们将查找字符串列表中存在的字典键。然后使用空列表,该列表可用于以列表的形式存储结果并将其存储在变量 avl_keys 中。接下来,使用 for 循环,变量 k 遍历变量 d_list(字典列表)。使用 if 语句,条件是给定的输入字典中是否存在字符串。然后使用内置方法 append() 查找字典键并返回到变量 avl_keys 并生成结果。
示例
def dict_keys(d_list, s_list):
avl_keys = []
for k in d_list:
if k in s_list:
avl_keys.append(k)
return avl_keys
# Create the dictionary and string list
dict1 = {'A': 11, 'B': 12, 'C': 13, 'D': 14, 'E': 15, 'F': 16}
stng = ['M', 'N', 'O', 'E', 'G', 'A', 'C', 'P', 'Q', 'R']
# Calling function
res = dict_keys(dict1, stng)
print("The result of dictionary keys present in a strings list:\n", res)
输出
The result of dictionary keys present in a strings list: ['A', 'C', 'E']
使用 filter 函数
在下面的示例中,我们将从一个接受两个参数 d 和 s 的递归函数开始程序,以从变量 dcn 和 stng 接收字典和字符串的原始输入。接下来,它将使用函数返回使用一些内置函数 lambda- 计算程序的主要操作,filter()- 获取字典中存在的那些值,以及 list()- 将结果格式化为列表。最后,它将以这种方式生成程序的输出。
示例
def dict_keys(d, s):
return list(filter(lambda k: k in s, d))
# Create the dictionary and string list
dcn = {'abc': 10, 'bcd': 20, 'mno': 30, 'xyz': 40, 'pqr': 50}
stng = ['ab', 'mno', 'bcs', 'xy']
# Calling Function
res = dict_keys(dcn, stng)
print("The result of dictionary keys present in a strings list:\n", res)
输出
The result of dictionary keys present in a strings list: ['mno']
结论
在每个示例中,字典和字符串都用于设置程序的原始输入,其中它设置为操作,即字符串列表中存在的键。使用了各种内置函数,例如 filter()、set()、keys() 等,以在需要时满足特殊需求。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP