Python 从字典列表中提取具有给定键的字典

Python 从字典列表中提取具有给定键的字典

Python对于一种更常见地被称为关联数组的数据结构的实现是 字典 。字典由一组键值对组成,每个键值组合都对应一个键和其相应的值。

在本文中,我们将学习一个Python程序,从字典列表中提取具有给定键的字典。

使用的方法

以下是实现此任务的各种方法:

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

  • 使用filter()和lambda函数

  • 使用operator.countOf()方法

示例

假设我们已经输入了一个包含字典的列表和一个输入键。现在我们将使用上述方法从包含给定输入键的列表中提取字典。

输入

inputList = [{'hello': 3, 'tutorialspoint': 6}, {'python': 9},
   {'users': 12, 'users': 15, 'hello': 18},
   {'users': 21}]
inputKey = 'users'

输出

The resultant list of dictionaries containing the 'users' key −
[{'users': 15, 'hello': 18}, {'users': 21}]

在上述输入的字典列表中,只有以下字典包含输入的键 {‘users’: 12, ‘users’: 15, ‘hello’: 18}, {‘users’: 21} 。因此,从输入列表中提取了这些字典。

使用列表推导和keys()函数

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

keys()函数(字典的keys()方法提供了一个显示按插入顺序排列的字典中所有键的列表的视图对象)

步骤

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

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

  • 打印输入列表。

  • 创建另一个变量来存储输入的键。

  • 使用列表推导遍历给定的字典列表,并使用in运算符检查键是否存在于字典的键中。

  • 如果为真,则将此字典存储在列表中。

  • 打印包含输入键的结果字典列表。

示例

以下程序使用列表推导和keys()函数从输入字典中返回包含输入键的字典列表:

# input list of dictionaries
inputList = [{'hello': 3, 'tutorialspoint': 6}, {'python': 9},
   {'users': 12, 'users': 15, 'hello': 18},
   {'users': 21}]
# printing input list
print("Input List:\n", inputList)
# input key
inputKey = 'users'
# Traversing in the given input list and checking 
# whether the input Key exists in the list of dictionaries keys
resultantDict = [e for e in inputList if inputKey in list(e.keys())]
# printing resultant list of dictionaries containing input key
print("The Resultant list of dictionaries containing the 'users' key:\n", resultantDict)

输出

Input List:
 [{'hello': 3, 'tutorialspoint': 6}, {'python': 9}, {'users': 15, 'hello': 18}, {'users': 21}]
The Resultant list of dictionaries containing the 'users' key:
 [{'users': 15, 'hello': 18}, {'users': 21}]

使用filter()和lambda函数

filter()函数 - 使用一个函数来确定序列中的每个元素是真还是假,从而对序列进行过滤。

lambda函数

lambda函数是一个匿名函数,它很简短。

lambda函数可以具有无限/任意数量的参数,但只能有一个表达式。

语法

lambda arguments : expression

示例

以下程序使用filter()和lambda函数从输入字典中返回包含输入键的字典列表 –

# input list of dictionaries
inputList = [{'hello': 3, 'tutorialspoint': 6}, {'python': 9},
   {'users': 12, 'users': 15, 'hello': 18},
   {'users': 21}]
# printing input list
print("Input List:\n", inputList)
# input key
inputKey = 'users'
# Filtering all the dictionaries with the given input key
resultantDict = list(
    filter(lambda sub: inputKey in list(sub.keys()), inputList))
# printing the resultant list of dictionaries containing input key
print("Resultant list of dictionaries containing 'users' key:\n", resultantDict)

输出

执行上述程序后,将生成以下输出 –

Input List:
 [{'hello': 3, 'tutorialspoint': 6}, {'python': 9}, {'users': 15, 'hello': 18}, {'users': 21}]
Resultant list of dictionaries containing 'users' key:
 [{'users': 15, 'hello': 18}, {'users': 21}]

使用operator.countOf()方法

operator模块的countOf()函数返回 a 中等于 b 的元素的数量。

语法

operator.countOf(a, b)

参数

  • a ― 列表或字符串或任何其他数据类型。

  • b ― “a”中要计算出现次数的值。

示例

以下程序使用列表推导和keys()函数返回一个包含输入字典中的输入键的字典列表 –

import operator as op
# input list of dictionaries
inputList = [{'hello': 3, 'tutorialspoint': 6}, {'python': 9},
   {'users': 12, 'users': 15, 'hello': 18},
   {'users': 21}]
# printing input list
print("Input List:\n", inputList)
# input key
inputKey = 'users'
# Traversing in the given input list and checking
# whether the input Key exists in the list of dictionaries keys
resultantDict = [e for e in inputList if op.countOf(
    list(e.keys()), inputKey) > 0]
# printing resultant list of dictionaries containing input key
print("The Resultant list of dictionaries containing the 'users' key:\n", resultantDict)

输出

在运行上面的程序时,会生成以下输出结果:

Input List:
 [{'hello': 3, 'tutorialspoint': 6}, {'python': 9}, {'users': 15, 'hello': 18}, {'users': 21}]
The Resultant list of dictionaries containing the 'users' key:
 [{'users': 15, 'hello': 18}, {'users': 21}]

结论

在这篇文章中,我们学习了如何使用三种不同的技术从字典列表中检索具有特定键的字典。已经学习了新的方法。使用operator.countOf()来计算可迭代对象的元素个数。为了确定元素是否存在,可以使用新的方法代替”in”和”not in”操作符。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程