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()函式檢查輸入字典的任何鍵的值是否包含給定的列表元素:

# 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()函式檢查輸入字典的任何鍵的值是否包含給定的列表元素:

# 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

結論

在本文中,我們學習了四種不同的方法來檢查任何鍵是否包含所有給定的列表元素。此外,我們還學習瞭如何使用pop()函式從字典中刪除鍵值對。

更新於:2023年8月17日

83 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始
廣告