2013-10-11 37 views
1

我有蟒蛇類型的字典2只列出比較http://stardict.sourceforge.net/Dictionaries.php下載字典的字典與附加值

子字典我的意思是一個至少有相同的鍵/值,但可能有額外的鍵/值的字典。

如此,例如:

realDict =  [{'a': 1, 'b': 2}, {'a': 2, 'b': 3}] 
== 
expectedDict = [{'a': 1},   {'a': 2, 'b': 3}] 


realDict =  [{'a': 1, 'b': 2}, {'a': 2, 'b': 3}] 
!= 
expectedDict = [{'a': 2},   {'a': 2, 'b': 3}] 


realDict =  [{'a': 1, 'b': 2}, {'a': 1, 'b': 3}] 
!= 
expectedDict = [{'a': 1}] 

任何模塊,以幫助?只有我能想到的想法是遍歷expectedDict,並在它們匹配時從它和realDict中刪除一個字典。然後,最後,兩者都必須是[]。

+0

是否爲了此事?必須'realDict [0]'總是匹配'expectedDict [0]'還是可能匹配'expectedDict [1]'? – mVChr

+0

你確定你的第一個例子是正確的嗎?對於expectedList的每個字典,在realList上有一個且只有一個「sub-dict」,這表明對於expectedList中的每個字典,在realList中有一個字典,它有一個額外的密鑰。儘管在第一個例子中情況並非如此,但你仍然稱它們爲 – inspectorG4dget

+0

@mVChr,訂單無關緊要 – Lem0n

回答

1

這工作,但我不能確定它是多麼高性能:

def complete_subset(real_list, expected_list): 
    real_set_list = [set(d.items()) for d in real_list] 
    expected_set_list = [set(d.items()) for d in expected_list] 

    while len(real_set_list): 
     real_len = len(real_set_list) 
     i = 0 
     for real_set in real_set_list: 
      for expected_set in expected_set_list: 
       if not len(expected_set - real_set): 
        real_set_list.remove(real_set) 
        expected_set_list.remove(expected_set) 
      i = i + 1 
     if i == real_len: 
      break 

    return (not len(real_set_list)) and (not len(expected_set_list)) 

這裏是我的測試:

print complete_subset([{'a': 1, 'b': 2}, {'a': 2, 'b': 3}], [{'a': 1}, {'a': 2, 'b': 3}]) == True 
print complete_subset([{'a': 1, 'b': 2}, {'a': 2, 'b': 3}], [{'a': 2}, {'a': 2, 'b': 3}]) == False 
print complete_subset([{'a': 1, 'b': 2}, {'a': 2, 'b': 3}], [{'a': 1}]) == False 
print complete_subset([{'a': 1, 'b': 2}, {'a': 1, 'b': 2}], [{'a': 1}, {'b': 2}]) == True 
print complete_subset([ 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10}, 
], [ 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}, 
    {'a': 1, 'b': 2, 'c': 3, 'd': 4}, 
    {'a': 1, 'b': 2, 'c': 3}, 
    {'a': 1, 'b': 2}, 
    {'a': 1}, 
    {'a': 1, 'c': 3, 'd': 4, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10}, 
    {'a': 1, 'b': 2, 'c': 3, 'e': 5, 'f': 6, 'h': 8, 'i': 9, 'j': 10}, 
    {'a': 1, 'b': 2, 'd': 4, 'e': 5, 'g': 7, 'h': 8, 'j': 10}, 
]) == True 
+0

是的,顯然沒有一個簡單的/「pythonic」的解決方案。我接受這個。謝謝 – Lem0n

+0

@Lem0n酷,我實際上想到了一種類似但更有效的方式來做這件事,同時在淋浴時想着。我還沒有完全用代碼完成它,但是讓我知道如果你不想弄清楚如何使它更有效率,那麼你是否希望我採取一個刺。 – mVChr

-1

如果你的兩個列表要逐個比較,你可以這樣做。

def compare(realDict, expectedDict): 
    if len(readDict) != len(expectedDict): 
     return False 
    for d1, d2 in zip(realDict, expectedDict): 
     for key in d2: 
      if key not in d1 or d1[key] != d2[key]: 
       return False 
    return True 
+0

這不允許「一個額外的密鑰」 – inspectorG4dget

+0

您不提及任何地方的「一個額外的密鑰」你的問題。你能澄清一些例子嗎? –

+0

另外,如果您提到realDict可以擁有更多密鑰這一事實,那麼我們只會檢查expectedDict的所有鍵是否都是realDict,而不是相反。 –