如何在嵌套的python字典中插入值



我正在尝试将值插入嵌套的python字典中。我得到的响应来自一个postgresql数据库,其中表有四列:

coe,  coe_type, count, coe_status
Author 1, Open,       10,    Published
Author 2, Closed,     20,    Not-Published
etc....

目前我的回复是这样的

"data": {
"Author 1": {
"Open": {},
"Closed": {},
"All": {
"Published": 1,
"Non-Published": 1
}
},

我遇到的问题是,我想插入每个特定类型的所有计数。例如,Open应该有自己的已发布和未发布计数。对于Closed也是一样。

那么响应应该是这样的

"data": {
"beryl": {
"Open": {
"Published": 1,
"Non-Published": 1
},
"Closed": {
"Published": 1,
"Non-Published": 1
},
"All": {
"Published": 1,
"Non-Published": 1
}
},

当前代码是这样写的:

category_headers = ["Open", "Published"]
book_main_headers = ["Owner", "Published", "Non-Published"]
book_type_headers=["Open", "Closed", "All"]
response_result = {}
last_author = None
response = execute_query(sql_query, transaction_id, False)
current_list = ast.literal_eval(row)
current_author, book, book_count, book_published_non_published = (current_list[0], current_list[1], current_list[2], current_list[3])
if last_author is None or last_author != current_author:
interim_dictionary[str(current_author)] = {} 
last_author = current_author
for book_type in book_type_headers:
interim_dictionary[str(current_author)][str(book_type)] = {} 
for coe_category in category_headers:
interim_dictionary[str(current_author)][str(book_type)][str(book_category)] = {} 
if book_category not in interim_dictionary[str(current_author)]:
interim_dictionary[str(current_author)][str(book_type)][book_category] = 0
if book == 'Open':
if book_type_headers == 'Published':
interim_dictionary[str(current_author)][str(book_type)][book_category] = book_count
else:
interim_dictionary[str(current_author)][str(book_type)][book_category] = book_count
if book == 'Closed':
if book_type_headers == 'Published':
interim_dictionary[str(current_author)][str(book_type)][book_category] = book_count
else:
interim_dictionary[str(current_author)][str(book_type)][book_category] = book_count
else:
if book_type_headers == 'Published':
interim_dictionary[str(current_author)][str(book_type)][book_category] = book_count
else:
interim_dictionary[str(current_author)][str(book_type)][book_category] = book_count

您的代码中有一些问题。首先,第二个for循环在第一个循环之外,这意味着book_type的值在其余代码中始终是All。此外,第19行上的if似乎没有检查正确的条件:不应该是book_category not in interim_dictionary[str(current_author)][str(book_type)]而不是book_category not in interim_dictionary[str(current_author)]吗?您还比较列表与字符串:if book_type_headers == 'Published':。最后,从第25行开始的所有行都做完全相同的事情,所以if是无用的。

下面是我写这段代码的方法:
category_headers = ["Open", "Published"]
book_main_headers = ["Owner", "Published", "Non-Published"]
book_type_headers=["Open", "Closed", "All"]
response_result = {}
last_author = None
response = execute_query(sql_query, transaction_id, False)
current_list = ast.literal_eval(row)
current_author, book_type, book_count, book_published_non_published = (current_list[0], current_list[1], current_list[2], current_list[3])
if last_author is None or last_author != current_author:
# Create empty dictionnary for the author
interim_dictionary[str(current_author)] = { book_type: { category: 0 for category in category_headers } for book_type in book_type_headers }
last_author = current_author
# Add books to 'All'
interim_dictionary[str(current_author)]['All'][book_published_non_published] += book_count
# Add books to the subdictionnary with proper type
interim_dictionary[str(current_author)][book_type][book_published_non_published] += book_count