[英]Delete key:value pair from dict in list in nested dict
Lets say I have this dict:可以说我有这个字典:
dictos = {
"a": {
"b": {
"c": 3,
"d": 4,
"e": [],
"f": [{"g": 5}, 'test', {"h": 6, "i": 7}]
}
}
}
And lets say I want to delete "c": 3 pair.假设我想删除 "c": 3 对。 What I am doing:
我在做什么:
import dpath.util
path = "a/b/c"
dpath.util.delete(dictos, path)
It is working great.它工作得很好。 The output is:
output 是:
{
'a': {
'b': {
'd': 4,
'e': [],
'f': [{'g': 5}, 'test', {'h': 6, 'i': 7}]
}
}
}
The problem is when I am trying to delete key:value pair inside the list.问题是当我试图删除列表中的键:值对时。 Lets say I want to delete "h":6.
比方说我想删除“h”:6。 So when doing:
所以在做的时候:
path = "a/b/f[2]/h"
dpath.util.delete(dictos, path)
I am getting:我正进入(状态:
dpath.exceptions.PathNotFound: Could not find a/b/f[2]/h to delete it.
dpath.exceptions.PathNotFound:找不到要删除的 a/b/f[2]/h。
So the question basically is how to delete items from nested dicts that are in a list?所以问题基本上是如何从列表中的嵌套字典中删除项目?
It seems the library expects the same separator to be used for all segments ie use a/b/f/2/h
似乎图书馆希望所有段都使用相同的分隔符,即使用
a/b/f/2/h
path = "a/b/f/2/h"
dpath.util.delete(dictos, path)
print(dictos)
Result:结果:
{'a': {'b': {'d': 4, 'e': [], 'f': [{'g': 5}, 'test', {'i': 7}]}}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.