Python 查找字典中在列表中存在的键的最大值
在Python中,我们可以使用简单的for循环、max、count和operator函数来查找字典中在列表中存在的键的最大值。
Python的数据结构实现更常被称为关联数组的是 字典 。字典由一组键值对组成。每个键值组合对应一个键和其对应的值。
示例
假设我们有一个 输入字典 和一个 输入列表 。现在我们将查找在输入列表中存在的键的输入字典中的最大值。
输入
inputDict = {"hello": 6, "tutorialspoint": 20, "python": 5, "codes": 15}
inputList = ["python", "dictionaries", "article", 'codes']
输出
Maximum value of dictionary where the key is in the list: 15
在上面的输入字典中, 最大 值是 20 ,对应的键是 tutorialspoint 。但是单词 tutorialspoint 不在输入列表中。因此,下一个最大值是进行检查的,即 codes 也在列表中。
因此,输出结果是15。
使用for循环
步骤
以下是执行所需任务的算法/步骤:
- 创建一个变量来存储 输入字典 。
-
创建另一个变量来存储 输入列表 。
-
初始化一个变量 (maxValue) 为0,以存储字典的最大值。
-
使用 for循环 遍历输入列表的每个元素。
-
使用 if条件语句 来检查当前元素是否存在于字典的键中。
-
使用 max() 函数,获取上述初始化的 maxValue 变量值与字典的当前键值的最大值。
-
打印输入字典中具有输入列表中存在的键的最大值。
示例
以下程序使用for循环和max()函数从输入字典中返回键也存在于输入列表中的最大值:
# input dictionary
inputDict = {"hello": 6, "tutorialspoint": 20, "python": 5, "codes": 15}
# printing input dictionary
print("Input dictionary:", inputDict)
# input list
inputList = ["python", "dictionaries", "article", 'codes']
# intializing with 0 for storing resultant max value
maxValue = 0
# traversing through each element of the input list
for e in inputList:
# checking whether the current element is present in the keys of a dictionary
if e in inputDict:
# getting the max value from the maxValue variable and
# the current key value of the dictionary
maxValue = max(maxValue, inputDict[e])
# printing the resultant maximum value
print("Maximum value of dictionary where the key is in the list:", maxValue)
输出
执行上述程序后,将生成以下输出:
Input dictionary: {'hello': 6, 'tutorialspoint': 20, 'python': 5, 'codes': 15}
Maximum value of dictionary where the key is in the list: 15
使用列表推导和max()函数
当你希望基于现有列表的值构建一个新的列表时,列表推导提供了一种更短、更简洁的语法。
max()方法 - 返回可迭代对象中最大值的项/最大数。
示例
以下程序使用列表推导和max()函数,返回一个输入字典中键也存在于输入列表中的最大值:
# input dictionary
inputDict = {"hello": 6, "tutorialspoint": 20, "python": 5, "codes": 15}
# input list
inputList = ["python", "dictionaries", "article", 'codes']
# Get all the elements Values that are present in both dictionary and the list
# Get the max of this list using the max() function
maxValue = max([inputDict[e] for e in inputList if e in inputDict])
# printing the resultant maximum value
print("Maximum value of dictionary where the key is in the list:", maxValue)
输出
Maximum value of dictionary where the key is in the list: 15
使用Counter()函数
Counter()函数 - 一个子类,计算可哈希对象的数量。在调用/调用时,它会隐式地创建一个可迭代的哈希表。
在这个方法中,我们使用 Counter()函数 将列表元素的频率作为键值对返回。
示例
以下程序使用Counter()函数返回一个输入字典中的最大值,其键也存在于输入列表中 –
# importing a Counter function from the collections module
from collections import Counter
# input dictionary
inputDict = {"hello": 6, "tutorialspoint": 20, "python": 5, "codes": 15}
# input list
inputList = ["python", "dictionaries", "article", 'codes']
# getting the frequency of input list elements as a key-value pair
listFrequency = Counter(inputList)
# intializing with 0 for storing resultant max value
maxValue = 0
# traversing through each element of the input list
for p in inputDict:
# checking whether the current element is present in the keys
# of above list frequency
if p in listFrequency.keys():
# getting the max value from the maxValue variable
# and the current key value of dictionary
maxValue = max(maxValue, inputDict[p])
# printing the resultant maximum value
print("Maximum value of dictionary where the key is in the list:", maxValue)
输出
Maximum value of dictionary where the key is in the list: 15
使用operator.countOf()方法
在这个方法中,我们将使用Python中operator库的countOf()函数来找到字典中的最大值。
语法
operator.countOf(a, b)
operator模块的countOf()函数返回 a 中与 b 相等的元素的数量。
示例
以下程序使用列表推导式和operator.countOf()函数返回输入字典中与输入列表中的键相对应的最大值 –
import operator as op
# input dictionary
inputDict = {"hello": 6, "tutorialspoint": 20, "python": 5, "codes": 15}
# input list
inputList = ["python", "dictionaries", "article", 'codes']
maxValue = max([inputDict[e]
for e in inputList if op.countOf(inputDict, e) > 0])
# printing the resultant maximum value
print("Maximum value of dictionary where the key is in the list:", maxValue)
输出
Maximum value of dictionary where the key is in the list: 15
结论
本文介绍了四种不同的技巧,用于找出包含在列表中的键对应的字典中的最大值。新方法还介绍了使用operator.countOf()函数来确定可迭代对象的元素个数。