Python 程序提取键数量最多的字典


Python 中,一个更常见地称为关联数组的数据结构的实现是**字典**。字典由一组键值对组成。每个键值组合对应一个键及其对应的值。

在本文中,我们将学习一个 Python 程序来提取键数量最多的字典。

使用的方法

以下是完成此任务的各种方法

  • 使用 for 循环和 len() 函数

  • 使用列表推导式和 max() 函数

示例

假设我们已经获取了一个包含多个字典的**输入列表**。我们现在将使用上述方法提取具有最大键数的字典。

输入

inputList = [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15}, {'good': 3, 'website': 9}]

输出

Resultant dictionary having maximum no of keys:
[{'hello': 5, 'tutorialspoint': 10, 'users': 15}]

在上面的示例中,第二个字典**{'hello': 5, 'tutorialspoint': 10, 'users': 15}**具有最大数量的键,即**3**。因此,它从输入字典中提取。

使用 for 循环和 len() 函数

len() 方法返回对象中的项目数。当对象是字符串时,len() 函数返回字符串中的字符数。

算法(步骤)

以下是执行所需任务的算法/步骤。

  • 创建一个变量来存储包含**字典**的**输入列表**。

  • 打印输入列表。

  • 创建一个新的空字典来存储结果字典。

  • 将最大长度初始化为 0 以存储最大长度。

  • 使用**for 循环**遍历输入列表的每个元素。

  • 使用**if 条件**语句来检查当前元素的长度是否大于使用**len()**函数(返回对象中的项目数)的最大长度。

  • 如果条件为真,则将该当前元素分配给结果字典。

  • 使用 len() 函数将当前元素的长度作为最大长度。

  • 打印输入列表中键数量最多的结果字典。

以下程序使用 for 循环和 len() 函数返回输入字典列表中键数量最多的字典。

# input list containing dictionaries
inputList = [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15},
   {'good': 3, 'website': 9}]
# printing input list
print("Input list of dictionaries:\n", inputList)
# creating a new empty dictionary
resultantDict = dict()
# initializing with 0 for storing maximum length
maxLength = 0
# traversing through each element of the input list
for p in inputList:
    # checking whether the length of the current element is
    # greater than the maximum length
    if len(p) > maxLength:
        # assigning that current element to the resultant dictionary
        resultantDict = p
		# assigning the length of a current element as the maximum length
        maxLength = len(p)
# printing resultant dictionary having a maximum no of keys in the input list
print("Resultant dictionary having maximum no of keys:\n", resultantDict)

输出

执行上述程序将生成以下输出。

Input list of dictionaries:
 [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15}, {'good': 3, 'website': 9}]
Resultant dictionary having maximum no of keys:
 {'hello': 5, 'tutorialspoint': 10, 'users': 15}

使用列表推导式和 max() 函数

当您希望基于现有列表的值构建新列表时,列表推导式提供了一种更短/简洁的语法。

max() 函数(返回可迭代对象中值最高的项目/最大数)

算法(步骤)

以下是执行所需任务的算法/步骤。

  • 创建一个变量来存储包含**字典**的**输入列表**。

  • 打印输入列表。

  • 使用列表推导式遍历输入字典列表并获取字典的长度。

  • 使用 max() 函数查找输入列表中字典的最大长度。

  • 使用列表推导式遍历输入字典列表并获取具有此长度的字典。

  • 打印输入列表中键数量最多的结果字典。

示例

以下程序使用列表推导式和 max() 函数返回输入列表中键数量最多的字典。

# input list containing dictionaries
inputList = [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15},
    {'good': 3, 'website': 9}]
# printing input list of dictionaries
print("Input list of dictionaries:\n", inputList)
# getting the lengths of each dictionary element using list comprehension
# getting the maximum length from this list using the max() function
maxLength = max(len(p) for p in inputList)
# Finding the dictionary with the length as the maximum length
resultantDict = [p for p in inputList if len(p) == maxLength]
# printing resultant dictionary having maximum no. of keys in the input list
print("Resultant dictionary having maximum no of keys:\n", resultantDict)

输出

执行上述程序将生成以下输出。

Input list of dictionaries:
 [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15}, {'good': 3, 'website': 9}]
Resultant dictionary having maximum no of keys:
 [{'hello': 5, 'tutorialspoint': 10, 'users': 15}]

结论

在本文中,我们学习了如何使用两种不同的方法提取键数量最多的字典。为了快速解决问题,我们还学习了如何使用列表推导式以及简单、简洁的语法。

更新于: 2023年8月18日

89 次查看

开启您的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.