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

算法(步骤)

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

  • 创建一个变量来存储输入字典并打印它。

  • 创建另一个变量来存储输入列表。

  • 将结果初始化为False。

  • 使用for循环遍历输入字典的键。

  • 使用if条件语句,使用issuperset()函数检查当前键的对应值是否出现在输入列表中。

  • 如果条件为真,则将结果更新为True。

  • 打印结果。

示例1:使用for循环和issuperset()函数

issuperset()函数(如果给定集合中的所有元素都存在于原始集合中,则返回True;否则,返回False)

以下程序使用for循环和issuperset()函数检查输入字典的任何键的值是否包含给定的列表元素:

Open Compiler
# 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()函数:如果iterable中的任何项目为真,则返回True;否则返回False。

以下程序使用any()和issuperset()函数检查输入字典的任何键的值是否包含给定的列表元素:

Open Compiler
# 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

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

示例3:不使用issuperset()函数

以下程序在不使用issuperset()函数的情况下检查输入字典的任何键的值是否包含给定的列表元素

Open Compiler
# 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:使用递归

以下程序使用递归检查输入字典的任何键的值是否包含给定的列表元素

Open Compiler
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

结论

在本文中,我们学习了四种不同的方法来检查任何键是否包含所有给定的列表元素。此外,我们还学习了如何使用pop()函数从字典中删除键值对。

更新于:2023年8月17日

83 次浏览

启动您的职业生涯

完成课程获得认证

开始
广告