簡體   English   中英

用相應的鍵比較字典?

[英]Comparing dictionaries by corresponding keys?

我是Python的初學者。

我有兩個字典,其中每個鍵是一個產品,每個值是每個產品的價格。

productData_1 = {'product_1': '18', 'product_2': '15', 'product_3': '10', 'product_4': '9'}
productData_2 = {'product_1': '18', 'product_3': '12', 'product_2': '15'}

我想做的是按鍵和按值比較兩個字典,以便我可以看到兩個字典之間的差異,例如價格是否不同, productData_1_2是否缺少productData_1

我知道我必須以某種方式遍歷這兩個字典,但是我不知道該怎么做。

在Python 3.5中:

productData_1 = {'product_1': '18', 'product_2': '15', 'product_3': '10', 'product_4': '9'}
productData_2 = {'product_1': '18', 'product_3': '12', 'product_2': '15'}

keys = set (productData_1.keys () + productData_2.keys ())

for key in keys:
    try:
        if productData_1 [key] != productData_2 [key]:
            print ('Mismatch at key {}'.format (key))
    except:
        print ('Entry missing at key {}'.format (key))
for key in productData_1.keys()|productData_2.keys():
    if key not in productData_1:
        print('{} missing from productData_1!'.format(key))
        continue
    if key not in productData_2:
        print('{} missing from productData_2!'.format(key))
        continue
    print('Difference in prices for key "{}": {}'.format(key,abs(int(productData_1[key])-int(productData_2[key]))))

如果任一字典缺少鍵,則將打印此代碼,並打印每個鍵的值之間的絕對差。

productData_1.keys()|productData_2.keys()將返回字典鍵的並集。

keys_unique_to_1 = [key for key in productData_1 if key not in productData_2]
keys_unique_to_2 = [key for key in productData_2 if key not in productData_1]
common_keys = [key for key in productData_1 if key in productData_2]

diff = {key: int(productData_1[key]) - int(productData_2[key]) for key in common_keys}

print 'Keys unique to 1: ', keys_unique_to_1
# Keys unique to 1:  ['product_4']

print 'Keys unique to 2: ', keys_unique_to_2
# Keys unique to 2:  []

print 'Difference: ', diff
# Difference:  {'product_1': 0, 'product_3': -2, 'product_2': 0}

關於此問題, 這里有一個很好的頁面。 您可以遍歷兩個詞典並比較它們的值。 下面的示例顯示了如何打印值相等的鍵。

================================================== =======================

productData_1 = {'product_1':'18','product_2':'15','product_3':'10','product_4':'9'} productData_2 = {'product_1':'18','product_3': '12','product_2':'15'}

對於Python 2.x-

for key_1, value_1 in productData_1.iteritems():
    for key_2, value_2 in productData_2.iteritems():
    if value_1 == value_2:
         print("key_1" + str(key_1))
         print("key_2" + str(key_2))

對於Python 3.x-

for key_1, value_1 in productData_1.items():
     for key_2, value_2 in productData_2.items():
     if value_1 == value_2:
         print("key_1" + str(key_1))
         print("key_2" + str(key_2))
productData_1 = {'product_1': '18', 'product_2': '15', 'product_3': '10', 'product_4': '9'}
productData_2 = {'product_1': '18', 'product_3': '12', 'product_2': '15'}

matched_keys = []
unmatched_keys = []
matched_value = []
unmatched_value = []

for ind,value in productData_1.items():
    if ind in productData_2.keys():
        matched_keys.append( ind )
        if value in productData_2.values():
            matched_value.append( value )
        else:
            unmatched_value.append( value )
    else:
        unmatched_keys.append( ind )
        if value in productData_2.values():
            matched_value.append( value )
        else:
            unmatched_value.append( value )



print matched_keys
print unmatched_keys

print matched_value
print unmatched_value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM