2014-12-04 719 views
3

我試圖通過比較兩個字典比較鍵,如果兩個單獨的字典中的兩個鍵是相同的程序應該檢查值是否也是同樣的,如果它們不一樣,那麼程序應該識別這個。比較Python中的兩個字典通過識別具有相同鍵但不同值的集

這是我所編寫的代碼:

def compare(firstdict,seconddict): 
    shared_items = set(firstdict()) & set(seconddict()) 
    length = len(shared_items) 
    if length > 0: 
     return shared_items 
    if length < 1: 
     return None 
print(compare(firstdict,seconddict)) 

(「firstdict」和「seconddict」是已在先前的函數已進行了兩個字典)。

當代碼運行時,即使它們的值不同,也會打印出沒有相同值的所有鍵。

例如,如果:

firstdict = {'cat' : 'animal', 'blue' : 'colour', 'sun' : 'star'} 

seconddict = {'cat' : 'pet', 'blue' : 'colour', 'earth' : 'star'} 

它會打印出:

'cat', 'blue' 

而我特林把它打印出來:在正確的格式

'cat pet (animal)' 

如何編輯我的代碼來做到這一點的任何建議表示讚賞:)

回答

4

您可以在字典keys()使用交集。然後遍歷這些並檢查對應於這些鍵的值是否相同。如果沒有,可以用format打印出來。

def compare(first, second): 
    sharedKeys = set(first.keys()).intersection(second.keys()) 
    for key in sharedKeys: 
     if first[key] != second[key]: 
      print('Key: {}, Value 1: {}, Value 2: {}'.format(key, first[key], second[key])) 

>>> compare(firstdict, seconddict) 
Key: cat, Value 1: animal, Value 2: pet 

而對於另一個例子

>>> firstdict = {'cat' : 'animal', 'blue' : 'colour', 'sun' : 'star', 'name': 'bob', 'shape': 'circle'} 
>>> seconddict = {'cat' : 'pet', 'blue' : 'colour', 'earth' : 'star', 'name': 'steve', 'shape': 'square'} 

>>> compare(firstdict, seconddict) 
Key: shape, Value 1: circle, Value 2: square 
Key: cat, Value 1: animal, Value 2: pet 
Key: name, Value 1: bob, Value 2: steve 
0

如果值是可哈希還可以使用的物品,以獲得共同的鍵/值配對:

firstdict = {'cat' : 'animal', 'blue' : 'colour', 'sun' : 'star'} 

seconddict = {'cat' : 'pet', 'blue' : 'colour', 'earth' : 'star'} 

common = set(firstdict.iteritems()).intersection(seconddict.iteritems()) 

for k,v in common: 
    print("Key: {}, Value: {}".format(k,v)) 
Key: blue, Value: colour 

要檢查這兩個類型的字典是同樣,檢查每個的長度:

print(len(common)) == len(firstdict) 

要找到不同的價值共同鍵:

for k,v in firstdict.iteritems(): 
    if k in seconddict and seconddict[k] != v: 
     print("Key: {}, Value: {}".format(k, seconddict[k])) 
Key: cat, Value: pet 
0

在您的示例代碼中使用的套使得它比簡單循環的鑰匙,這也是,或許,更具可讀性效率較低:

firstdict = {'cat' : 'animal', 'blue' : 'colour', 'sun' : 'star'} 
seconddict = {'cat' : 'pet', 'blue' : 'colour', 'earth' : 'star'} 

def compare(firstdict, seconddict): 
    for key in firstdict: 
     if key in seconddict: 
      first, second = firstdict[key], seconddict[key] 
      if first != second: 
       print('%s %s (%s)' % (key, first, second)) 

compare(firstdict, seconddict) 

輸出:

cat animal (pet) 
0

好帕德里克的方法似乎比這更好,但它是另一種選擇。所以這不是最好的方法,但它會完成工作。

逐步通過每個元素並手動比較它們。

def compare(dictOne,dictTwo): 
    for keyOne in dictOne: 
     for keyTwo in dictTwo: 
      if keyTwo == keyOne: 
       if dictOne[keyOne] != dictTwo[keyTwo]: 
        print(keyOne+" "+dictOne[keyOne]+" ("+dictTwo[keyTwo]+")") 

compare(firstdict,seconddict) 

你的問題說,這是想打印出來,並沒有提到你希望他們在返回數組所以上面的代碼將逐步通過兩個類型的字典比較它們一個接一個,並在格式打印出一個不匹配。

+0

顯然ekhumoro打我,而我暫停了電話...... – 2014-12-04 20:54:11

+0

ekhumoro的迴應可能是更有效的資源。 – 2014-12-04 20:56:48

相關問題