将多个字典作为值插入到单个键中
To insert multiple dictionaries as values into a single key
我需要一组 {a:{}{b:{},{c:{}}}} 形式的嵌套字典,其中 a、b 和 c 是键。我试过下面的代码。
from collections import defaultdict
def dictizeString(string,dictionary) :
while string.startswith('/'):
string = string[1:]
parts = string.split('/',1)
if len(parts)>1:
branch = dictionary.setdefault(parts[0],[dict()])
dictionary[parts[0]].append(dict())
dictizeString(parts[1], branch)
else:
if dictionary.has_key(parts[0]):
dictionary[parts[0]]=dict()
else:
dictionary[parts[0]]=[dict()]
dictionary[parts[0]].append(dict())
d={}
dictizeString('/a/b/c/d', d)
print d
执行此代码会导致错误“'list' 对象没有属性 'setdefault'”。该代码适用于第一次迭代(即 a),但在第二次迭代(即 b)时抛出上述错误。
追加功能适用于 code.I 最后 6 行中的 else 部分,试图在 if 情况下使用相同的逻辑,但它抛出错误。
尝试:
from collections import defaultdict
def dictizeString(string,dictionary) :
while string.startswith('/'):
string = string[1:]
parts = string.split('/',1)
if len(parts)>1:
branch = dictionary.setdefault(parts[0],[dict()])
dictionary[parts[0]].append(dict())
dictizeString(parts[1], branch[1]) # <--- branch -> branch[1]
else:
if dictionary.has_key(parts[0]):
dictionary[parts[0]]=dict()
else:
dictionary[parts[0]]=[dict()]
dictionary[parts[0]].append(dict())
d={}
dictizeString('/a/b/c/d', d)
print d
您在第 7 行的语句将默认值设置为字典的 列表 ,但随后您尝试坚持使用需要字典的函数。
我知道你已经找到了答案,但你也可以这样做:
def dictizeString(path, dictionary):
keys = path.lstrip('/').split('/')
current_key = None
for key in keys:
if current_key is None:
current_key = dictionary.setdefault(key, {})
else:
current_key = current_key.setdefault(key, {})
d = {}
dictizeString('/a/b/c/d', d)
print d
输出变为:{'a': {'b': {'c': {'d': {}}}}}
我需要一组 {a:{}{b:{},{c:{}}}} 形式的嵌套字典,其中 a、b 和 c 是键。我试过下面的代码。
from collections import defaultdict
def dictizeString(string,dictionary) :
while string.startswith('/'):
string = string[1:]
parts = string.split('/',1)
if len(parts)>1:
branch = dictionary.setdefault(parts[0],[dict()])
dictionary[parts[0]].append(dict())
dictizeString(parts[1], branch)
else:
if dictionary.has_key(parts[0]):
dictionary[parts[0]]=dict()
else:
dictionary[parts[0]]=[dict()]
dictionary[parts[0]].append(dict())
d={}
dictizeString('/a/b/c/d', d)
print d
执行此代码会导致错误“'list' 对象没有属性 'setdefault'”。该代码适用于第一次迭代(即 a),但在第二次迭代(即 b)时抛出上述错误。
追加功能适用于 code.I 最后 6 行中的 else 部分,试图在 if 情况下使用相同的逻辑,但它抛出错误。
尝试:
from collections import defaultdict
def dictizeString(string,dictionary) :
while string.startswith('/'):
string = string[1:]
parts = string.split('/',1)
if len(parts)>1:
branch = dictionary.setdefault(parts[0],[dict()])
dictionary[parts[0]].append(dict())
dictizeString(parts[1], branch[1]) # <--- branch -> branch[1]
else:
if dictionary.has_key(parts[0]):
dictionary[parts[0]]=dict()
else:
dictionary[parts[0]]=[dict()]
dictionary[parts[0]].append(dict())
d={}
dictizeString('/a/b/c/d', d)
print d
您在第 7 行的语句将默认值设置为字典的 列表 ,但随后您尝试坚持使用需要字典的函数。
我知道你已经找到了答案,但你也可以这样做:
def dictizeString(path, dictionary):
keys = path.lstrip('/').split('/')
current_key = None
for key in keys:
if current_key is None:
current_key = dictionary.setdefault(key, {})
else:
current_key = current_key.setdefault(key, {})
d = {}
dictizeString('/a/b/c/d', d)
print d
输出变为:{'a': {'b': {'c': {'d': {}}}}}