2016-08-03 79 views
0

我是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缺少的產品?

我知道我必須以某種方式遍歷這兩個字典,但我無法弄清楚如何去做。

+0

看看這個http://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops-in-python – giosans

+1

什麼是你想要的輸出? –

+1

你必須更清楚地定義你想要的輸出是什麼。在你的文章中,你有多個問題:你想要結果「product_4」(因爲它是唯一一個不在這兩個詞典中)或「product_3」(因爲它的值在兩個詞典中是不同的),還是兩者的結合? – Cristina

回答

0

在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)) 
0
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()將返回字典鍵的聯合。

0
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} 
0

在這個問題上有很好的頁面here。你可以遍歷你的字典並比較它們的值。下面的示例顯示瞭如何打印您的值相同的鍵。

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

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)) 
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'} 

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