是否可以從字典中刪除值而不是密鑰?例如, 我有以下代碼,其中用戶選擇與他想要刪除它的元素對應的鍵,但我只想刪除該值而不是鍵(該值是一個列表):Python從字典中刪除值而不是密鑰
if (selection == 2):
elem = int(input("Please select the KEY that you want to be deleted: "))
if dictionar.has_key(elem):
#dictionar.pop(elem)
del dictionar[elem]
else:
print("the KEY is not present ")
如果你什麼'dictionar [ELEM] = None' –
'has_key'已被棄用。使用'elem in dictionar'。 –
只需設置等於None常量的值。 – Frogboxe