取出内部字典的键和值,删除Python中的内部字典

时间:2018-02-28 12:25:36

标签: python dictionary

我有这样的字典:

{'some_company_100': {'key1': 'value1',
  'key2': 'value2',
  'key3': 'value3',
  'key4': 'value4',
  'key5': 'value5',
  'key6': {'key6_1': 'value6_1', 'key6_2': 'value6_2'},
  'key7': 'value7'},
 'some_company_101': {'key1': 'valuea',
  'key2': 'valueb',
  'key3': 'valuec',
  'key4': 'valued',
  'key5': 'valuee',
  'key6': {'keyf_1': 'valuef_1', 'keyf_2': 'valuef_2'},
  'key7': 'value7'}}

我需要把这些内部字典取出来摆脱嵌套字典。 我试过的方式:

for key, value in final_dict.iteritems():
    for k, v in value.copy().iteritems():
        if isinstance(v, dict):
            value.update(v)
            del[k]

但问题是内部字典中的迭代必须在字典的copy()上进行,所以我不能删除k,如果v在出现v后在我的原始字典上存在。如果我排除.copy()来自value.copy()。iteritems()。它返回一个错误:`RuntimeError:字典在迭代期间改变了大小。 值得一提的是,我试图将其设为通用,而不是使用

if k=='key6':
    do something

期望的输出:

{'some_company_100': {'key1': 'value1',
  'key2': 'value2',
  'key3': 'value3',
  'key4': 'value4',
  'key5': 'value5',
  'key6_1': 'value6_1',
  'key6_2': 'value6_2',
  'key7': 'value7'},
 'some_company_101': {'key1': 'valuea',
  'key1': 'valueb',
  'key2': 'valuec',
  'key4': 'valued',
  'key5': 'valuee', 
  'key6_1': 'value6_a', 
  'key6_2': 'value6_b'
  'key7': 'value7'}}

2 个答案:

答案 0 :(得分:0)

您可以创建一个新词典,并使用临时词典添加新值。这将允许原始字典保持不变。

示例:

result = {}
for inner in d:
    temp = {}
    for key in d[inner]:
        curr = d[inner][key]
        if isinstance(curr, dict):
            temp.update(curr)
        else:
            temp[key] = curr

    result[inner] = temp

print(result)

哪个输出:

{'some_company_100': {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4', 'key5': 'value5', 'key6_1': 'value6_1', 'key6_2': 'value6_2', 'key7': 'value7'}, 'some_company_101': {'key1': 'valuea', 'key2': 'valueb', 'key3': 'valuec', 'key4': 'valued', 'key5': 'valuee', 'keyf_1': 'valuef_1', 'keyf_2': 'valuef_2', 'key7': 'value7'}}

答案 1 :(得分:0)

在这里,虽然我认为它不是最优的,但这段代码的效果却令人满意! :d

import copy

dictionnary = {'some_company_100': {'key1': 'value1',
                      'key2': 'value2',
                      'key3': 'value3',
                      'key4': 'value4',
                      'key5': 'value5',
                      'key6': {'key6_1': 'value6_1', 'key6_2': 'value6_2'},
                      'key7': 'value7'},
 'some_company_101': {'key1': 'valuea',
                      'key2': 'valueb',
                      'key3': 'valuec',
                      'key4': 'valued',
                      'key5': 'valuee',
                      'key6': {'keyf_1': 'valuef_1', 'keyf_2': 'valuef_2'},
                      'key7': 'value7'}}
keys_to_delete = []
keys_to_add = []
for company in dictionnary:
    for key in dictionnary[company]:
        if type(dictionnary[company][key]) == type({}):
            for nested_key in dictionnary[company][key]:
                keys_to_add.append((company, copy.copy(nested_key), copy.copy(dictionnary[company][key][nested_key])))
            keys_to_delete.append((company, key))

for key in keys_to_delete:
    del dictionnary[key[0]][key[1]]


for key in keys_to_add:
    dictionnary[key[0]][key[1]] = key[2]
print(dictionnary)