Python 中列表和字典的公共键
在本文中,我们将学习如何在 python 中查找列表和字典中的公共键。
使用的方法
以下是完成此任务的各种方法 -
使用“in”运算符和列表推导式
使用 set(),intersection() 函数
使用 keys() 函数和 in 运算符
使用 Counter() 函数
示例
假设我们已经获取了一个输入字典和列表。我们将使用上述方法查找输入列表和字典键中的公共元素。
输入
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}
inputList = ["hello", "tutorialspoint", "python"]
输出
Resultant list: ['hello', 'tutorialspoint']
在上面的示例中,“hello”和“tutorialspoint”是输入列表和字典键中的公共元素。因此,它们被打印出来。
方法 1:使用“in”运算符和列表推导式
列表推导式
当您希望基于现有列表的值构建一个新列表时,列表推导式提供了一种更短/简洁的语法。
Python“in”关键字
in 关键字以两种方式工作 -
in 关键字用于确定一个值是否存在于一个序列(列表、范围、字符串等)中。
它也用于在 for 循环中迭代一个序列
算法(步骤)
以下是执行所需任务需遵循的算法/步骤 -。
创建一个变量来存储输入字典。
创建另一个变量来存储输入列表。
遍历输入列表并检查任何输入列表元素是否与使用列表推导式的字典键匹配。
打印结果列表。
示例
以下程序使用“in”运算符和列表推导式返回输入列表和字典键中的公共元素 -
# input dictionary
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}
# printing input dictionary
print("Input dictionary:", inputDict)
# input list
inputList = ["hello", "tutorialspoint", "python"]
# printing input list
print("Input list:", inputList)
# checking whether any input list element matches the keys of a dictionary
outputList = [e for e in inputDict if e in inputList]
# printing the resultant list
print("Resultant list:", outputList)
输出
执行后,上述程序将生成以下输出 -
Input dictionary: {'hello': 2, 'all': 4, 'welcome': 6, 'to': 8, 'tutorialspoint': 10}
Input list: ['hello', 'tutorialspoint', 'python']
Resultant list: ['hello', 'tutorialspoint']
方法 2:使用 set(),intersection() 函数
set() 函数 - 创建一个集合对象。集合列表将以随机顺序显示,因为项目没有排序。它删除所有重复项。
intersection() 函数 - intersection() 方法返回一个包含两个或多个集合之间相似性的集合。
这意味着仅包含同时存在于两个集合中,或者如果比较了两个以上的集合,则存在于所有集合中的项目。
示例
以下程序使用 set() 和 intersection() 返回输入列表和字典键中的公共元素 -
# input dictionary
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}
# printing input dictionary
print("Input dictionary:", inputDict)
# input list
inputList = ["hello", "tutorialspoint", "python"]
# printing input list
print("Input list:", inputList)
# Converting the input dictionary and input List to sets
# getting common elements in input list and input dictionary keys
# from these two sets using the intersection() function
outputList = set(inputList).intersection(set(inputDict))
# printing the resultant list
print("Resultant list:", outputList)
输出
执行后,上述程序将生成以下输出 -
Input dictionary: {'hello': 2, 'all': 4, 'welcome': 6, 'to': 8, 'tutorialspoint': 10}
Input list: ['hello', 'tutorialspoint', 'python']
Resultant list: {'hello', 'tutorialspoint'}
方法 3:使用 keys() 函数和 in 运算符
keys() 函数 - dict.keys() 方法提供一个视图对象,该对象按插入顺序显示字典中所有键的列表。
示例
以下程序使用 keys() 函数和 in 运算符返回输入列表和字典键中的公共元素 -
# input dictionary
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}
# printing input dictionary
print("Input dictionary:", inputDict)
# input list
inputList = ["hello", "tutorialspoint", "python"]
# printing input list
print("Input list:", inputList)
# empty list for storing the common elements in the list and dictionary keys
outputList = []
# getting the list of keys of a dictionary
keysList = list(inputDict.keys())
# traversing through the keys list
for k in keysList:
# checking whether the current key is present in the input list
if k in inputList:
# appending that key to the output list
outputList.append(k)
# printing the resultant list
print("Resultant list:", outputList)
输出
Input dictionary: {'hello': 2, 'all': 4, 'welcome': 6, 'to': 8, 'tutorialspoint': 10}
Input list: ['hello', 'tutorialspoint', 'python']
Resultant list: ['hello', 'tutorialspoint']
方法 4:使用 Counter() 函数
Counter() 函数 - 一个计算可散列对象的子类。它在被调用/调用时隐式地创建可迭代对象的哈希表。
这里 Counter() 函数用于获取输入列表元素的频率。
示例
以下程序使用 Counter() 函数返回输入列表和字典键中的公共元素 -
# importing a Counter function from the collections module
from collections import Counter
# input dictionary
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}
# printing input dictionary
print("Input dictionary:", inputDict)
# input list
inputList = ["hello", "tutorialspoint", "python"]
# printing input list
print("Input list:", inputList)
# getting the frequency of input list elements as a dictionary
frequency = Counter(inputList)
# empty list for storing the common elements of the list and dictionary keys
outputList = []
# getting the list of keys of a dictionary
keysList = list(inputDict.keys())
# traversing through the keys list
for k in keysList:
# checking whether the current key is present in the input list
if k in frequency.keys():
# appending/adding that key to the output list
outputList.append(k)
# printing the resultant list
print("Resultant list:", outputList)
输出
Input dictionary: {'hello': 2, 'all': 4, 'welcome': 6, 'to': 8, 'tutorialspoint': 10}
Input list: ['hello', 'tutorialspoint', 'python']
Resultant list: ['hello', 'tutorialspoint']
结论
我们在本文中研究了四种不同的方法来显示给定列表和字典中的公共键。我们还学习了如何使用 Counter() 函数获取可迭代对象频率的字典。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP