Python 提取拥有最大数量键的词典

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()函数返回对象中的项目数)。

  • 如果条件为真,则将当前元素赋值给结果字典。

  • 使用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}]

结论

在这篇文章中,我们学习了如何使用两种不同的方法提取具有最大键数量的字典。为了快速解决问题,我们还学习了如何使用列表推导和简洁的语法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程