如何使用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’运算符和列表推导式返回输入列表和字典键中的公共元素 –
# 输入字典
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}
# 打印输入字典
print("Input dictionary:", inputDict)
# 输入列表
inputList = ["hello", "tutorialspoint", "python"]
# 打印输入列表
print("Input list:", inputList)
# 检查任何输入列表元素是否与字典键匹配
outputList = [e for e in inputDict if e in inputList]
# 打印出结果列表
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() 函数 – 返回一个包含两个或多个集合之间的相似性的集合。这意味着在返回的集合中只包括同时存在于两个或多个集合中的项目。
示例
以下程序使用set()和intersection()返回输入列表和字典键中的公共元素 –
# 输入的字典
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}
# 打印输入字典
print("输入字典: ", inputDict)
# 输入列表
inputList = ["hello", "tutorialspoint", "python"]
# 打印输入列表
print("输入列表: ", inputList)
# 将输入字典和输入列表转换为集合
# 使用intersection()函数从这两个集合中获取公共元素
outputList = set(inputList).intersection(set(inputDict))
# 打印结果列表
print("结果列表: ", outputList)
输出
运行以上程序将生成以下输出 –
输入字典: {'hello': 2, 'all': 4, 'welcome': 6, 'to': 8, 'tutorialspoint': 10}
输入列表: ['hello', 'tutorialspoint', 'python']
结果列表: {'hello', 'tutorialspoint'}
方法3:使用keys()函数和in运算符
keys()函数 − dict. keys() 方法提供了一个显示按插入顺序列出字典中所有键的列表的视图对象。
例子
以下程序使用keys()函数和in运算符返回输入列表和字典中的键的公共元素 –
# 输入的字典
inputDict = {"hello":"2", "all":"4", "welcome":"6", "to":"8", "tutorialspoint":"10"}
# 打印输入字典
print("输入字典:", inputDict)
# 输入列表
inputList = ["hello", "tutorialspoint", "python"]
# 打印输入列表
print("输入列表:", inputList)
# 空列表用于存储列表和字典键中的公共元素
outputList = []
# 获取字典键的列表
keysList = list(inputDict.keys())
# 遍历键列表
for k in keysList:
# 检查当前键是否存在于输入列表中
if k in inputList:
# 将该键添加到输出列表中
outputList.append(k)
# 打印结果列表
print("结果列表:", outputList)
输出
输入字典: {'hello': 2, 'all': 4, 'welcome': 6, 'to': 8, 'tutorialspoint': 10}
输入列表: ['hello', 'tutorialspoint', 'python']
结果列表: ['hello', 'tutorialspoint']
方法4:使用Counter()函数
Counter()函数 − 一个子类,用于计算可哈希对象。当调用时,它会隐式地创建可迭代对象的哈希表。
这里使用Counter()函数获取输入列表元素的频率。
例子
以下程序使用Counter()函数返回输入列表和字典键中的公共元素 –
# 从collections模块导入Counter函数
from collections import Counter
# 输入的字典
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}
# 打印输入字典
print("输入字典:", inputDict)
# 输入列表
inputList = ["hello", "tutorialspoint", "python"]
# 打印输入列表
print("输入列表:", inputList)
# 获取输入列表元素的频率作为一个字典
frequency = Counter(inputList)
# 空列表用于存储列表和字典键中的公共元素
outputList = []
# 获取字典键的列表
keysList = list(inputDict.keys())
#遍历键列表
for k in keysList:
# 检查当前键是否存在于频率键中
if k in frequency.keys():
# 将该键添加到输出列表中
outputList.append(k)
# 打印结果列表
print("结果列表:", outputList)
输出
输入字典: {'hello': 2, 'all': 4, 'welcome': 6, 'to': 8, 'tutorialspoint': 10}
输入列表: ['hello', 'tutorialspoint', 'python']
结果列表: ['hello', 'tutorialspoint']
结论
在本文中,我们学习了四种不同的方法来显示给定列表和字典中的公共键。我们还学习了如何使用Counter()函数获取可迭代对象频率的字典。