Объединение словарей в питоне

select_attr1 = 'SELECT top (1) Attributes from [apprepo].[00207BA8-BD1C-49B1-B001-FACA805E374E_objects]'
crsr1.execute(select_attr1)
for a in crsr1.execute(select_attr1):
   for q in a:
       # print(q)
       dct =json.loads(q)
       # print(dct)
       for d in dct.values():
           for k in d:
               print(k)

Получаю словари типа:

{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'exportSourceAnchor'}
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'AlternateEmailAddress'}
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'AlternateEmailAddress'}
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'IACPreferredLanguage'}
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'IACClientID'}
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'AS_AuthenticationAddress'}
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'Assistant'}
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'C'}
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 36887, 'PendingInboundValue': 36887, 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'CloudMSExchArchiveStatus'}
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'AACQFw==', 'PendingInboundValue': 'AACQFw==', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'CloudMSExchBlockedSendersHash'}
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 36887, 'PendingInboundValue': 36887, 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'CloudMSExchRecipientDisplayType'}
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'AACQFw==', 'PendingInboundValue': 'AACQFw==', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'CloudMSExchSafeRecipientsHash'}
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'AACQFw==', 'PendingInboundValue': 'AACQFw==', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'CloudMSExchSafeSendersHash'}

А хотелось бы все в один словарь выводить. Или нельзя потому что много ключей будут одинаковыми и будет конфликт?


Ответы (2 шт):

Автор решения: Dmitry

Конфликта как такового не будет, но будет перезапиываться значение под одинковым ключом.

Поведение в коде выглядит вот так

j = """
[{"1":"first"}, {"2":"second"}, {"1":"first_new"}]
"""
import json 

lst_ = json.loads(j)
result_dict = {}
for item in lst_:
    for k, v in item.items():
        result_dict[k] = v

print(result_dict)

Никаких ошибок не будет, но результатом будет - потеря данных значения первого словаря

{'1': 'first_new', '2': 'second'}

И судя по тому как вы разбираете данные, у вас в dct содержится некая последовательность. Предположу, что это список (list). Что вполне релевантно для работы в python. Может работать с этим списком?

→ Ссылка
Автор решения: CameL

Можно сделать словарь, в котором к каждому ключу будет прилагаться список уникальных значений. Если нужны все значения, то уберите эти строки

if v not in temp:
   temp.append(v)
   result[k] = temp

Полный код:

d_list = [{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'exportSourceAnchor'},
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'AlternateEmailAddress'},
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'AlternateEmailAddress'},
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'IACPreferredLanguage'},
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'IACClientID'},
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'AS_AuthenticationAddress'},
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'Assistant'},
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'fznpqxtofs', 'PendingInboundValue': 'fznpqxtofs', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'C'},
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 36887, 'PendingInboundValue': 36887, 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'CloudMSExchArchiveStatus'},
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'AACQFw==', 'PendingInboundValue': 'AACQFw==', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'CloudMSExchBlockedSendersHash'},
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 36887, 'PendingInboundValue': 36887, 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'CloudMSExchRecipientDisplayType'},
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'AACQFw==', 'PendingInboundValue': 'AACQFw==', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'CloudMSExchSafeRecipientsHash'},
{'LastImportUpdateTS': '2022-07-28T11:30:43.7197802Z', 'Value': 'AACQFw==', 'PendingInboundValue': 'AACQFw==', 'PendingExportValue': None, 'LastOutboundUpdateTs': None, 'IACDeletedTS': None, 'AttributeName': 'CloudMSExchSafeSendersHash'}]
result = {}
for d in d_list:
    for k,v in d.items():
        if k not in result:
            result[k] = [v] 
        else:
            temp = result[k]
            if v not in temp:
                temp.append(v)
                result[k] = temp

for k,v in result.items():
    print(k,v)
→ Ссылка