Python 检查任何键是否具有所有给定的列表元素
一个用方括号括起来的逗号分隔值(项)的数组(列表)是Python中最灵活的数据类型之一。列表的元素不必是相同类型的事实非常重要。
在本文中,我们将学习一个Python程序,以检查任何键是否具有所有给定的列表元素。
示例
假设我们有一个输入字典和一个列表。现在我们将使用上述方法检查输入字典的任何键的值是否包含给定的列表元素。
输入
inputDict = {'hello': [4, 2, 8, 1],
'tutorialspoint': [5, 12, 10, 6],
'python': [9, 3, 7, 1],
'users': [3, 6, 1, 8, 2]}
inputList = [1, 2, 3]
输出
Do the values of any key have given list elements?: True
在上述的字典中,只有 ‘users’ 键包含所有输入列表元素 1, 2, 3 的值。因此结果是 True。
步骤如下:
1. 创建一个变量来存储输入的字典并打印它。
2. 创建另一个变量来存储输入的列表。
3. 将结果初始化为 False。
4. 用 for 循环遍历输入字典的键。
5. 使用 if 条件语句来检查当前键的相应值是否在输入列表中使用 issuperset() 函数。
6. 如果条件为真,则将结果更新为 True。
7. 打印结果。
示例 1:使用 for 循环和 issuperset() 函数
issuperset() 函数(如果给定集合中的所有元素都在原始集合中,则返回 True;否则返回 False) 以下程序使用 for 循环和 issuperset() 函数检查输入字典的任何键的值是否包含给定的列表元素。
# input dictionary
inputDict = {'hello': [4, 2, 8, 1],
'tutorialspoint': [5, 12, 10, 6],
'python': [9, 3, 7, 1],
'users': [3, 6, 1, 8, 2]}
# printing input dictionary
print("Input dictionary:\n", inputDict)
# input list
inputList = [1, 2, 3]
# initializing result as False
result = False
# traversing through the keys of the input dictionary
for k in inputDict:
# checking whether the corresponding value of the current key is
# present in the input list using issuperset() function
if set(inputDict[k]).issuperset(inputList):
# updating the result as True if the condition is true
result = True
# printing the result
print("Do the values of any key have given list elements?:", result)
输出
Input dictionary:
{'hello': [4, 2, 8, 1], 'tutorialspoint': [5, 12, 10, 6], 'python': [9, 3, 7, 1], 'users': [3, 6, 1, 8, 2]}
Do the values of any key have given list elements?: True
示例2:使用any()和issuperset()函数
any()函数: 如果可迭代对象中有任何一个元素为真,则返回True,否则返回False。
下面的程序使用any()和issuperset()函数来检查输入字典的任何键的值是否包含给定列表元素 –
# input dictionary
inputDict = {'hello': [4, 2, 8, 1],
'tutorialspoint': [5, 12, 10, 6],
'python': [9, 3, 7, 1],
'users': [3, 6, 1, 8, 2]}
# printing input dictionary
print("Input dictionary:\n", inputDict)
# input list
inputList = [1, 2, 3]
# Checking if the key has all list elements
result = any(set(val).issuperset(inputList)
for val in inputDict.values())
# printing the result
print("Do the values of any key have given list elements?:", result)
输出
Input dictionary:
{'hello': [4, 2, 8, 1], 'tutorialspoint': [5, 12, 10, 6], 'python': [9, 3, 7, 1], 'users': [3, 6, 1, 8, 2]}
Do the values of any key have given list elements?: True
示例3:不使用issuperset()函数
下面的程序检查输入字典的任何键的值是否包含给定的列表元素,而不使用issuperset()函数。
# creating a function to check key contains all list elements
# by accepting dictionary value, input list as arguments
def containsListElements(a,b):
cnt =0
for k in b:
if k in a:
cnt+=1
if(cnt==len(b)):
return True
return False
# input dictionary
inputDict = {'hello': [4, 2, 8, 1],
'tutorialspoint': [5, 12, 10, 6],
'python': [9, 3, 7, 1],
'users': [3, 6, 1, 8, 2]}
# input list
inputList = [1, 2, 3]
# initializing result as False
result = False
# traversing through the keys of input dictionary
for k in inputDict.keys():
# calling the above defined contains() function
if(containsListElements(inputDict[k],inputList)):
# updating the result as True if the condition is true
result=True
# breaking the loop
break
# printing the result
print("Do the values of any key have given list elements?:", result)
输出
Do the values of any key have given list elements?: True
示例4:使用递归
以下程序使用递归来检查输入字典中任何键的值是否包含给定的列表元素
def containsListElements(a, b, result):
if len(b) == 0:
return result
if b[0] in a:
return containsListElements(a, b[1:], True)
else:
return containsListElements(a, b[1:], False)
def recursiveFunc(inputDict, inputList, result):
# returning the result directly if the length of the input dictionary is 0
if len(inputDict) == 0:
return result
# removing the key value from the given dictionary
key, val = inputDict.popitem()
if containsListElements(val, inputList, False):
# updating the result as True if the condition is true
result = True
# recursive logic
return recursiveFunc(inputDict, inputList, result)
# input dictionary
inputDict = {'hello': [4, 2, 8, 1],
'tutorialspoint': [5, 12, 10, 6],
'python': [9, 3, 7, 1],
'users': [3, 6, 1, 8, 2]}
# input list
inputList = [1, 2, 3]
result = recursiveFunc(inputDict, inputList, False)
# printing the result
print("Do the values of any key have given list elements?:", result)
输出
Do the values of any key have given list elements?: True
结论
在本文中,我们学习了4种不同的方法来检查是否有任何键具有所有给定的列表元素。另外,我们还学习了如何使用pop()函数从字典中删除键-值对。