Created
December 14, 2021 16:24
-
-
Save chapov/d5d0db9e4aabc7e0d301889c339da1fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections.abc | |
def dict_gen_from_list_keys(cur, list_keys, value, type_value: str = "list"): | |
""" | |
Сгенерирует словарь из списка ключей. | |
Generate dict from list keys | |
list = ['a','b','c'] | |
value = 'value' | |
out = {'a':{'b':{'c': 'value'} | |
""" | |
if len(list_keys) == 1: | |
if type_value == "list": | |
cur[list_keys[0]] = [] | |
cur[list_keys[0]].append(value) | |
else: | |
cur[list_keys[0]] = value | |
return | |
if list_keys[0] not in cur: | |
cur[list_keys[0]] = {} | |
dict_gen_from_list_keys(cur[list_keys[0]], list_keys[1:], value) | |
def dict_merge(dct, merge_dct): | |
""" | |
Рекурсивное слияние словаря. | |
recursion merge dicts | |
""" | |
for k, v in merge_dct.items(): | |
if ( | |
k in dct | |
and isinstance(dct[k], dict) | |
and isinstance(merge_dct[k], collections.Mapping) | |
): | |
dict_merge(dct[k], merge_dct[k]) | |
else: | |
dct[k] = merge_dct[k] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment