Python - Matching dictionaries with similar key-value pair

时间:2019-02-21 04:59:49

标签: python dictionary key-value

I am trying to match dictionaries using the key y2 of line texts and store them under a new dictionary key Transaction.

This is my input:

a = [[{'Line No ': '1', 'Line Bounding Box ': '(15, 2, 170, 79)', 'Lowest Confidence ': '0', 'Line texts ': [{'Column_No': '2', 'text_item': 'stuff1','y2':100}]}],[{'Line No ': '1', 'Line Bounding Box ': '(15, 2, 170, 79)', 'Lowest Confidence ': '0', 'Line texts ': [{'Column_No': '1', 'text_item': 'stuff2','y2':100}]}],[{'Line No ': '1', 'Line Bounding Box ': '(15, 2, 170, 79)', 'Lowest Confidence ': '0', 'Line texts ': [{'Column_No': '5', 'text_item': 'stuff3','y2':101}]}]]

This is the logic I have used:

sorted_lst = []

''' Getting each dict row by row '''
for i in a:
    #print(i)
    x = (i[0]['y2'])
    #print(x)
    sorted_lst.append(x)
    #print(sorted_lst)
    sorted_lst = sorted(list(set(sorted_lst)))
    c = []
    for k in sorted_lst:
        temp_dict1 = {}
        if x == k:
            temp_key1 = "column" + i[0]["Column_No"]
            temp_dict1[temp_key1] = i[0]["text_item"]
            #print(temp_dict1)
        c.append({'y2':k,'Transactions':temp_dict1})
from pprint import pprint

pprint(c)

This is the current output where its printing null for first matching dictionary:

[{'Transactions': {}, 'y2': 100},
 {'Transactions': {'column5': 'stuff3'}, 'y2': 101}]

This is the desired output:

[{'Transactions': {'column2': 'stuff1',
                   'column1': 'stuff2'},
  'y2': 100},
 {'Transactions': {'column5': 'stuff3'},
  'y2': 101}]

Where exactly am I going wrong with my logic?

1 个答案:

答案 0 :(得分:0)

考虑到a中的列表项只有一个字典作为其对应项,我们可以首先在列表中附加所有不同的y2值,然后检查遍历列表a的搜索表示字典内y2键内'Line texts '键的值,它是列表'a'内列表的唯一项。

尝试一下:

a = [[{'Line No ': '1', 'Line Bounding Box ': '(15, 2, 170, 79)', 'Lowest Confidence ': '0', 'Line texts ': [{'Column_No': '2', 'text_item': 'stuff1','y2':100}]}],[{'Line No ': '1', 'Line Bounding Box ': '(15, 2, 170, 79)', 'Lowest Confidence ': '0', 'Line texts ': [{'Column_No': '1', 'text_item': 'stuff2','y2':100}]}],[{'Line No ': '1', 'Line Bounding Box ': '(15, 2, 170, 79)', 'Lowest Confidence ': '0', 'Line texts ': [{'Column_No': '5', 'text_item': 'stuff3','y2':101}]}]]
b = []
for i in a:
    b.append(i[0]["Line texts "][0]['y2'])
b = sorted(list(set(b)))
print(b) # [100, 101]
c = []
for k in b:
    temp_dict ={}
    for i in a:
        if i[0]["Line texts "][0]['y2'] == k:
            temp_key = "column" + str(i[0]["Line texts "][0]["Column_No"])
            temp_dict[temp_key] = i[0]["Line texts "][0]["text_item"]
    c.append({"Transactions" : temp_dict, "y2" : k})
print(c)
"""
[{'Transactions': {'column1': 'stuff2', 'column2': 'stuff1'}, 'y2': 100},
 {'Transactions': {'column5': 'stuff3'}, 'y2': 101}]
"""

您的代码应该给定KeyError,因为a的列表项是单个词典的另一个列表,没有任何键'y2'的项,但是您正在尝试访问大字典不存在的键。