【发布时间】: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