更改多级字典值的更多 pythonic 方法

More pythonic way to change multilevel dictionary values

例如,我有以下字典:

{'foo': 'test', 
 'bar': {'test1': 'some text here'},
 'baz': {'test2': {'test3': 'some text here', 'test4': 'some text here'}}}

或者类似这样的东西,也许更多级别。

现在的问题是,我想把...'some text here'改成'A test'。是的,我可以像下面的代码一样使用很多 for 循环:

d = {'foo': 'test',
     'bar': {'test1': 'some text here'},
     'baz': {'test2': {'test3': 'some text here',
                       'test4': 'some text here'}}}

for i in d:
    if d[i] == 'some text here':
        d[i] = 'A test'

    elif type(d[i]) == dict:
        for j in d[i]:
            if d[i][j] == 'some text here':
                d[i][j] = 'A test'

            elif type(d[i][j]) == dict:
                for n in d[i][j]:
                    if d[i][j][n] == 'some text here':
                        d[i][j][n] = 'A test'

__import__('pprint').pprint(d)

输出:

{'bar': {'test1': 'A test'},                                                    
 'baz': {'test2': {'test3': 'A test', 'test4': 'A test'}},
 'foo': 'test'}

但是我认为这不是一个好方法...有什么想法吗?

这看起来是递归的好例子。

import re

def replace_rec(data, search, replace, _pattern=None):
    if _pattern is None:
        _pattern = re.compile(r'^%s$' % search)
    for k, v in data.items():
        try:
            data[k] = _pattern.sub(replace, v)
        except TypeError:
            try:
                replace_rec(data[k], search, replace, _pattern=_pattern)
            except AttributeError:
                # Leave any other types as they are.
                continue

像这样在您的示例中使用它:

>>> data = {
...     'foo': 'test',
...     'bar': {'test1': 'some text here'},
...     'baz': {'test2': {'test3': 'some text here', 'test4': 'some text here'}},
...     'loc': [1, 2, 3],
...     'fp': 'foo some text here foo',
... }
>>> replace_rec(data, 'some text here', 'A test')
>>> pprint.pprint(data)
{'bar': {'test1': 'A test'},
 'baz': {'test2': {'test3': 'A test', 'test4': 'A test'}},
 'foo': 'test',
 'fp': 'foo some text here foo',
 'loc': [1, 2, 3]}

稍微另类的版本。这会正确处理字典中的非字符串,并且只会替换完全匹配的文本。

def replace(d, find_text, replace_text):
    for k, v in d.items():
        if isinstance(v, dict):
            replace(v, find_text, replace_text)
        elif isinstance(v, str):
            if v == find_text:
                d[k] = replace_text

d = {
    'test': 'dont change some text here',
    'ignore' : 42,

    'foo': 'test', 
    'bar': {'test1': 'some text here'},
    'baz': {'test2': {'test3': 'some text here', 'test4': 'some text here'}}}       

replace(d, 'some text here', 'A test')

__import__('pprint').pprint(d)

这将显示:

{'bar': {'test1': 'A test'},
 'baz': {'test2': {'test3': 'A test', 'test4': 'A test'}},
 'foo': 'test',
 'ignore': 42,
 'test': 'dont change some text here'}

递归答案都很有趣和游戏,但您可以通过注意字典是可变的来获得更多 Pythonic:

首先获取对所有词典(所有级别)的引用,然后更改它们。

def all_dicts(d):
    yield d
    for v in d.values():
        if isinstance(v, dict):
            yield from all_dicts(v)

data = dict(foo=12, bar=dict(dd=12), ex=dict(a=dict(b=dict(v=12))))

for d in all_dicts(data):
     for k, v in d.items():
         if v == 12:
             d[k] = 33 

只要您要更改的值不是太毛茸茸,这里有一条线:

In [29]: data = dict(foo=12, bar=dict(dd=12), ex=dict(a=dict(b=dict(v=12))))

In [30]: json.loads(re.sub("12", "33", json.dumps(data)))
Out[30]: {'bar': {'dd': 33}, 'ex': {'a': {'b': {'v': 33}}}, 'foo': 33}

编辑,根据 Kevin 的说法,简单的替换甚至不需要 re:

json.loads(json.dumps(data).replace("12", "33"))