【发布时间】:2016-05-04 07:40:25
【问题描述】:
所以我有以下字典:
my_dict{'key1': 'value1',
'key2': 'value2',
'key3': json.dumps([
{"**subkey1**": "subvalue1", "**subkey2**": "subvalue2",},
{"**subkey1**": "other_subvalue", "**subkey2**":"other_subvalue2"}])
}
我需要以某种方式创建一个我必须检查的 def,并为每个 subkey2 仅更改 def 本身的值
和所有的subkey1检查它的值是否和第二个subkey1一样 请注意,我说的只是 subkey1,我有两次。
我不想手动设置它们。意味着我有这个 dict 全局,并从许多 def 调用它,所以我需要进行这些更改并检查每个 def 内部
我尝试的是:
def recurse_keys(my_dict, indent = ''):
print(indent+str(key))
if isinstance(my_dict[key], dict):
recurse_keys(my_dict[key], indent+' ')
recurse_keys(my_dict)
现在它只打印我所有的参数,但不知道如何继续
例子:
my_dict{'name': 'georgi',
'famili': 'ivanov',
'drinks': json.dumps([
{"breakfast": "milk", "lunch": "beer",},
{"breakfast": "tea", "lunch":"vodka"}])
def test()
....check if both breakfast are the same and if not make them so....(all these, mean dict and the function it self are in same file)
所以我需要检查两个早餐的值是否相同(不知道它们),如果它们不一样,则使它们相同。
还要检查是否有空值或 0 的午餐,如果没有,则再次检查
【问题讨论】:
-
您是否只是想让它以一种易于准备的方式打印出字典?您提到了更改 - 但您的示例函数没有更改 - 这是预期的吗?
-
是的,它没有进行更改,因为我不知道如何进行更改....我需要更改第二个值,并比较第一个值。但不知道如何
-
如果您所关心的只是使“早餐”子键的值相同,那么为什么不同时指定“0”或“牛奶”或其他值呢?如果您没有对它们做任何事情,为什么还需要检查这些值?
标签: python json dictionary nested