简体   繁体   English

递归检查键的存在并将其追加到字典数组

[英]Check existence of a key recursively and append to array of dict

I've a dict as follows 我有一个命令如下

{
   "key1" : "value1",
   "key2" : "value2",
   "key3" : "value3",
   "key4" : {
       "key5" : "value5"
   }
}

If the dict has key1==value1, I'll append the dict into a list. 如果字典具有key1 == value1,我将把字典追加到列表中。

Suppose key1==value1 is not present in the first key value pair, whereas it is inside nested dict as follows: 假设key1 == value1在第一个键值对中不存在,而在嵌套dict中,如下所示:

{
   "key2" : "value2",
   "key3" : "value3",
   "key4" : {
       "key5" : "value5",
       "key1" : "value1",
       "key6" : { 
          "key7" : "value7",
          "key1" : "value1"
       }
   },
   "key8" : { 
       "key9" : "value9",
       "key10" : {
            "key11" : "value11",
            "key12" : "value12",
            "key1" : "value1"
       }
   }
}

In the above dict, I've to check first whether there is key1=value1. 在上面的字典中,我首先要检查是否有key1 = value1。 If not, I've to traverse the nested dict and if it found in the nested dict, I've to append that dict to the list. 如果没有,我必须遍历嵌套字典,如果它在嵌套字典中找到,则必须将该字典追加到列表中。 If the nested dict is also a nested dict but key1=value1 is find in the first key value pair, then no need to check the inner dict(Eg key4 has key1=value1 in the in the first key value pair. Hence no need to check the inner one eventhough key6 has key1=value1). 如果嵌套dict也是嵌套dict,但在第一个键值对中找到key1 = value1,则无需检查内部dict(例如key4在第一个键值对中具有key1 = value1。因此,无需即使key6具有key1 = value1),也要检查内部。

So finally, I'll have the list as follows. 最后,我将得到以下列表。

[
   {
       "key5" : "value5",
       "key1" : "value1",
       "key6" : { 
          "key7" : "value7",
          "key1" : "value1"
       }
   },
   {
            "key11" : "value11",
            "key12" : "value12",
            "key1" : "value1"
   }
]

How to achieve this? 如何实现呢? Note: The depth of the dict may vary 注意:字典的深度可能会有所不同

if a dict contains key1 and value1 we will add it to the list and finish. 如果字典包含key1value1我们将其添加到列表中并完成操作。 if not, we will got into all the values in the dict that are dict and do the same logic as well 如果没有,我们将陷入所有的字典是值dict ,做同样的逻辑,以及

l = []
def append_dict(d):
    if d.get("key1") == "value1":
        l.append(d)
        return

    for k,v in d.items():
        if isinstance(v, dict):
            append_dict(v) 


append_dict(d)
print l

an iterative solution will be adding to queue the dict we would like to check: 一个迭代的解决方案将添加我们要检查的字典到队列:

from Queue import Queue
q = Queue() 
l = []
q.put(d)
while not q.empty():
    d = q.get()
    if d.get("key1") == "value1":
        l.append(d)
        continue
    for k,v in d.items():
        if isinstance(v, dict):
            q.put(v) 

print l

As @shashank noted, usinq a stack instead of a queue will also work it is BFS vs DFS for searching in the dictionary 正如@shashank所指出的,usinq而不是queue也可以使用stack ,这是BFS vs DFS用于在字典中进行搜索

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM