我有一堆代表图形/决策树(2)的字符串(1)。每个字符代表沿图形的路径。
###1###
'0A1B'
'0A1A'
'0A0A'
'0A0A'
'0B10'
'0B11'
我想在Python中处理此字符串列表,以创建具有以下结构(3)的JSON变量:
###3###
{
"name": "0",
"count": 6,
"children": [
{
"name": "A",
"count": 4,
"children": [
{"name": "0",
"count": 2,
"children": [
{"name": "A", "count": 2}
]
},
{"name": "1",
"count": 2,
"children": [
{"name": "A", "count": 1},
{"name": "B", "count": 1}
]
}
]
},
{
"name": "B",
"count": 2,
"children": [
{"name": "1",
"count": 2,
"children": [
{"name": "0", "count": 1},
{"name": "1", "count": 1}
]
}
]
}
]
}
是否有可以使此操作变得容易的库?我可以使用json
库创建json对象,但不确定如何解析字符串。看来递归函数是必要的吗?
除了内置的json
,我认为您不需要任何特定的库:
import json
def dict_tree(ss):
# there has to be at least one string and it has to have at least 1 character
assert ss and ss[0]
result = {'name': ss[0][0], 'count': 0}
for s in ss:
# all strings should start with the same character
# (the suggested data structure does not support more than one name at the root level)
assert s and s[0] == result['name']
p = result
p['count'] += 1
for ch in s[1:]:
if 'children' not in p:
p['children'] = []
for child in p['children']:
if child['name'] == ch:
p = child
break
else:
p['children'].append({'name': ch, 'count': 0})
p = p['children'][-1]
p['count'] += 1
return result
def main():
strings = [
'0A1B',
'0A1A',
'0A0A',
'0A0A',
'0B10',
'0B11'
]
print(json.dumps(dict_tree(strings), indent=4))
main()