值說出我們有以下兩個字典中Python
:Python的 - 比較鍵
dict1 = {'a':1, 'b':2, 'c':3, 'd': 4}
dict2 = {'c':2, 'd':1, 'b':2, 'a':1}
現在,我假設在dict1
值是正確的值。我如何將dict2
與dict1
進行比較,如果dict2
中的密鑰值與dict1
中的密鑰值相似,程序將返回True
,如果不同,則返回False
?
謝謝。
值說出我們有以下兩個字典中Python
:Python的 - 比較鍵
dict1 = {'a':1, 'b':2, 'c':3, 'd': 4}
dict2 = {'c':2, 'd':1, 'b':2, 'a':1}
現在,我假設在dict1
值是正確的值。我如何將dict2
與dict1
進行比較,如果dict2
中的密鑰值與dict1
中的密鑰值相似,程序將返回True
,如果不同,則返回False
?
謝謝。
如果類似你的意思是相等的,那麼你可以直接對它們進行比較:
def compare_dictionaries(correct_dictionary,dictionary_to_check):
for key,correct_value in correct_dictionary.items():
if dictionary_to_check[key] != correct_value:
return False
return True
以上會,如果你缺少你需要檢查辭典的鍵,拋出一個KeyError
異常,並沒有採取任何措施如果你正在檢查的字典包含額外的密鑰,則處理。
你可以很容易地得到'KeyError'異常。 –
@vishes_shell我同意。 –
如果這是所期望的行爲,可以將它壓縮爲'all(在dict1.items())中鍵入dict2和dict2 [key] == val(key,val)''。 –
# Both dictionaries can have different keys
dict1 = {'a':1, 'b':2, 'c':3, 'd': 4} # correct dict
dict2 = {'c':2, 'd':1, 'b':2, 'a':1} # test dict
if all(map(lambda x: dict2.get(x, "dict2") == dict1.get(x, "dict1"), dict2)):
print "same"
else:
print "different"
測試用例#1給出 「相同」
dict1 = {'a':1, 'b':2, 'c':3, 'd': 4, 'e':5} # correct dict
dict2 = {'c':3, 'd':4, 'b':2, 'a':1} # test dict
試驗例#2給出 「不同」
dict1 = {'a':1, 'b':2, 'c':3, 'd': 4 } # correct dict
dict2 = {'c':3, 'd':4, 'b':2, 'a':1, 'e':5} # test dict
# Both dictionaries can have different keys
dict1 = {'a':1, 'b':2, 'c':3, 'd': 4} # correct dict
dict2 = {'c':2, 'd':1, 'b':2, 'a':1} # test dict
if set(dict1.keys()) == set(dict2.keys()) and set(dict1.values()) == set(dict2.values()):
print "same"
else:
print "different"
測試案例#1給出了 「不同」
dict1 = {'a':1, 'b':2, 'c':3, 'd': 4, 'e':5} # correct dict
dict2 = {'c':3, 'd':4, 'b':2, 'a':1} # test dict
測試案例#2給出了 「不同」
dict1 = {'a':1, 'b':2, 'c':3, 'd': 4 } # correct dict
dict2 = {'c':3, 'd':4, 'b':2, 'a':1, 'e':5} # test dict
我猜測dict1鍵:值是什麼一切都被比較。
dict2 = {'c':2, 'd':1, 'b':2, 'a':1}
def compareDict(d2):
dict1 = {'a':1, 'b':2, 'c':3, 'd': 4}
for key in d2:
if d2[key] == dict1[key]:
print("{} key has similar value of {}; It is True".format(key,
d2[key]))
else:
print("{} key does not have similar value as dict1 key, it has a
value of {}; It is False".format(key, d2[key]))
compareDict(dict2)
首先檢查密鑰是相同的使用xor
操作,然後與對應的dictionary
的key
檢查值。希望這會有所幫助。
dict1 = {'a':2, 'b':2, 'c':3, 'd': 4}
dict2 = {'c':2, 'd':1, 'b':2, 'a':1}
# XOR operator for for checking all key are same.
check_keys= set(dict1.keys())^set(dict2.keys())
keys = set(dict2.keys())
check = False
# a=0 if all key are same.
a = len(check_keys)
# if all key are same than check the value with corresponding key
if not a:
check = True
for key in keys:
if dict1[key] == dict2[key]:
pass
else:
check = False
break
print(check)
通過給出布爾標誌來比較字典的大小,鍵和值。
def compareDictionaries(source, target, compareValues):
## compare if they are same
if (source == target):
return True
## covers scenario where target has more keys than source
if (len(target) > len(source)):
print("target has more keys than source")
return False
## iterate over target keys to check if they exists in
## source and depending on compareValues flag compare key's
## values
for key, values in target.items():
if (key in source.keys()):
if (compareValues):
if(not (source[key] == target[key])):
print ("key(" + key + ") don't have same values!")
return False
else:
print ("key(" + key + ") not found in source!")
return False
return True
dict1 = {'a':1, 'b':2, 'c':3, 'd': 4}
dict2 = {'c':5, 'd':4, 'b':2, 'a':1}
if (compareDictionaries(dict1, dict2, False)):
print("Pass")
else:
print("Fail")
定義'相似'。 – zwer
'dict1 == dict2'? –
如果'dict1'的按鍵不在'dict2'中,輸出應該如何?如果'dict2'的按鍵不在'dict1'中? –