【问题标题】:Python - Matching dictionaries with similar key-value pairPython - 匹配具有相似键值对的字典
【发布时间】:2019-02-21 04:59:48
【问题描述】:

我正在尝试使用 line texts 的键 y2 匹配字典,并将它们存储在新的字典键 Transaction 下。

这是我的输入:

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

这是我使用的逻辑:

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)

这是当前输出,其中第一个匹配字典打印 null:

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

这是所需的输出:

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

我的逻辑到底哪里出了问题?

【问题讨论】:

    标签: python dictionary key-value


    【解决方案1】:

    考虑到a 中的列表项只有一个字典作为它们的对应项,我们可以首先将所有不同的y2 值附加到一个列表中,然后检查循环遍历列表a 搜索@ 的值987654324@ key inside 'Line texts ' key in the dictionary 作为列表'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' 的项目,但是您正在尝试访问不存在的键更大的字典。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 2016-01-28
      • 2021-12-24
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多