擅长:python、mysql、java
<p>假设您只想检查列表或字典的容器,并从具有<code>key = "password"</code>的字典条目中删除键值对</p>
<pre><code>#first copy the structure
new_data = copy.deepcopy(data)
#this is a recursive function.
#Heavily nested structures may fail due to recursion limit
def clean_hierarchy(ele):
#lists may contain dictionaries, so clean theses entries
if isinstance(ele,list):
for val in ele:
clean_hierarchy(val)
if isinstance(ele,dict):
#remove possible password entry
if "password" in ele:
ele.pop("password",None)
#dictionary may contain more dictionaries. Rinse and repeat!
for val in ele.values():
clean_hierarchy(val)
clean_hierarchy(new_data)
</code></pre>