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 库的 coutof() 函数来查找字典中的最大值。
语法
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() 函数,用于确定可迭代对象的元素计数。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP