我正在将 Open Street Map 中的一些 XML 数据解析为 JSON(稍后将上传到数据库)。它很大,所以我正在使用 iterparse。我有一些标签看起来像这样
<tag 'k'=stringA:stringB:stringC 'v'=value>
我想解析成
{stringA: {stringB: {stringC: value} } }
我已经用丑陋的硬编码做到了。但是,我想创建一个使用递归来解决相同问题的函数,以防“k”属性值包含任意多个“:”。
我创造了这个
def nestify(l):
"""Takes a list l and returns nested dictionaries where each element from
the list is both a key to the inner dictionary and a value to the outer,
except for the last element of the list, one which is only a value.
For best understanding, feed w = [ 'a', 'b', 'c', 'd', 'e', 'f', 'v'] to the
function and look at the output."""
n = len(l)
if n==2:
key = l[0]
value = l[1]
else:
key = l[0]
value = nestify(l[1:])
return {key:value}
哪个有效。 (您必须先将键和值放入列表中。)但不是真的,因为它总是会创建一个新字典。我需要它尊重以前的数据并进行整合。例如,如果我的解析器遇到
<tag 'k'=a:b:c 'v'=1 />
然后在同一个元素中
<tag 'k'=a:d:e 'v'=2 />
我需要它来制作
{a: {'b': {'c' : 1}, 'd' : {'e' : 2}}}
不是
{a: {'b' : {'c' : 1}}}
{a: {'d' : {'e' : 2}}}
我试过这段代码:
def smart_nestify(l, record):
n = len(l)
if n==2:
key = l[0]
value = l[1]
else:
key = l[0]
value = smart_nestify(l[1:], key)
if key not in record:
return {key:value}
else:
record[key] = value
return record
但它仍然会覆盖并只返回最新的记录。这是为什么?我该如何修复此代码?
最佳答案
这是一个“更聪明”的 nestify,它将列表 l
合并到字典 record
中:
def smarter_nestify(l, record):
if len(l) == 2:
return {l[0]: l[1]}
key = l.pop(0)
record[key] = smarter_nestify(l, record.get(key, {}))
return record
每次通过递归,它从列表l.pop(0)
中弹出第一个元素,并合并字典下一级的值record.get(key, {})
。
你可以像这样用两个列表来调用它:
l = ['a', 'b', 'c', 1]
record = smarter_nestify(l, {})
l = ['a', 'd', 'e', 2]
record = smarter_nestify(l, record)
print record
关于python - 如何在与现有记录集成时使用递归嵌套字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37014500/