我需要从一些大型嵌套词典中获取一些值。出于懒惰,我决定编写一个递归调用自身的函数,直到找到最后一个子元素,或者叶子为空。
由于有字典弹出,每次新调用都会生成一个新的字典,我想知道这是多么有效。
有什么建议吗?
def recursive_dict_get(item, string, default=False):
if not isinstance(item, dict):
return default
print "called with ", item, "and string", string
if "." in string:
attrs = string.split(".")
parent = attrs.pop(0)
rest = ".".join(attrs)
result = item.get(parent, None)
if result is None:
return default
else:
return recursive_dict_get(item.get(parent, default), rest, default)
else:
return item.get(string, default)
foo = {
"1": {
"2": {
"3": {
"4":{
"5": {
"6": {
"7": "juice"
}
}
}
}
}
}
}
print recursive_dict_get(foo, "1.2.3.4.5.6.7", False)
print "*" * 3
print recursive_dict_get(foo, "1.2.3.4.5.6", False)
print "*" * 3
print recursive_dict_get(foo, "1.3", False)
答案 0 :(得分:5)
我的一个建议是给split()
第二个论点。你可以做一些更简单的事情:
parent, rest = string.split(".", 1)
除此之外,我发现代码没有立即出现问题。
您也可以在没有递归的情况下执行此操作:
def recursive_dict_get(item, string, default=False):
for s in string.split('.'):
if (isinstance(item, dict) and s in item):
item = item[s]
else:
return default
return item
答案 1 :(得分:4)
是的,你的实现效率很低,即使它没有构建任何新的词典,也可能会返回很多现有的词典。无论如何,您可以将接受的答案Access python nested dictionary items via a list of keys调整为将您的访问功能调整为一行代码。这与J.F. Sebastian在his comment中提到的相似。我对它的看法是这样的:
def nonrecursive_dict_get(item, key_string, default=False):
return reduce(lambda d, k: d.get(k, default), key_string.split('.'), item)
print "*" * 3, 'using nonrecursive_dict_get()'
print nonrecursive_dict_get(foo, "1.2.3.4.5.6.7")
print "*" * 3
print nonrecursive_dict_get(foo, "1.2.3.4.5.6")
print "*" * 3
print nonrecursive_dict_get(foo, "1.3")
<强>更新强>
每当效率成为一个问题时,通常最好的办法是运行各种方法的基准。这是我多次使用的一个:
global_setup = """
foo = {
"1": {
"2": {
"3": {
"4": {
"5": {
"6": {
"7": "juice"
}
}
}
}
}
}
}
"""
testcases = {
"jay":
{ 'setup' : """
def recursive_dict_get(item, string, default=False):
if not isinstance(item, dict):
return default
if "." in string:
attrs = string.split(".")
parent = attrs.pop(0)
rest = ".".join(attrs)
result = item.get(parent, None)
if result is None:
return default
else:
return recursive_dict_get(item.get(parent, default), rest, default)
else:
return item.get(string, default)
""",
'code' : """
recursive_dict_get(foo, "1.2.3.4.5.6.7", False)
recursive_dict_get(foo, "1.2.3.4.5.6", False)
recursive_dict_get(foo, "1.3", False)
""",
},
"martineau":
{ 'setup' : """
def nonrecursive_dict_get(nested_dict, key_string, default=False):
return reduce(lambda d, k: d.get(k, default), key_string.split('.'), nested_dict)
""",
'code' : """
nonrecursive_dict_get(foo, "1.2.3.4.5.6.7", False)
nonrecursive_dict_get(foo, "1.2.3.4.5.6", False)
nonrecursive_dict_get(foo, "1.3", False)
""",
},
"J.F. Sebastian":
{ 'setup' : """
# modified to support 'default' keyword argument
def quick_n_dirty(nested_dict, key_string, default=False):
reduced = reduce(dict.get, key_string.split('.'), nested_dict)
return default if reduced is None else reduced
""",
'code' : """
quick_n_dirty(foo, "1.2.3.4.5.6.7", False)
quick_n_dirty(foo, "1.2.3.4.5.6", False)
quick_n_dirty(foo, "1.3", False)
""",
},
"arshajii":
{ 'setup' : """
def recursive_dict_get(item, string, default=False):
for s in string.split('.'):
if (isinstance(item, dict) and s in item):
item = item[s]
else:
return default
return item
""",
'code' : """
recursive_dict_get(foo, "1.2.3.4.5.6.7", False)
recursive_dict_get(foo, "1.2.3.4.5.6", False)
recursive_dict_get(foo, "1.3", False)
""",
},
"Brionius":
{ 'setup' : """
def getVal(d, keys, default):
keys = keys.split(".")
for key in keys:
try:
d = d[key]
except KeyError:
return default
return d
""",
'code' : """
getVal(foo, "1.2.3.4.5.6.7", False)
getVal(foo, "1.2.3.4.5.6", False)
getVal(foo, "1.3", False)
""",
},
}
import sys
from textwrap import dedent
import timeit
N = 100000
R = 3
# remove leading whitespace from all code fragments
global_setup = dedent(global_setup)
for testcase in testcases.itervalues():
for label, fragment in testcase.iteritems():
testcase[label] = dedent(fragment)
timings = [(name,
min(timeit.repeat(testcases[name]['code'],
setup=global_setup + testcases[name]['setup'],
repeat=R, number=N)),
) for name in testcases]
longest_name = max(len(t[0]) for t in timings)
print('fastest to slowest timings:\n'
' ({:,d} calls, best of {:d} repetitions)\n'.format(N, R))
ranked = sorted(timings, key=lambda t: t[1]) # sort by speed (fastest first)
for timing in ranked:
print("{:>{width}} : {:.6f} secs ({rel:>8.6f}x)".format(
timing[0], timing[1], rel=timing[1]/ranked[0][1], width=longest_name))
输出:
fastest to slowest timings:
(100,000 calls, best of 3 repetitions)
J.F. Sebastian : 1.287209 secs (1.000000x)
Brionius : 1.420099 secs (1.103239x)
arshajii : 1.431521 secs (1.112112x)
martineau : 2.031539 secs (1.578251x)
jay : 7.817713 secs (6.073384x)
正如你所看到的,J.F。Sebastian的建议是最快的,即使我做了修改以使其与其他人一样。
答案 2 :(得分:1)
这是另一种方式:
def getVal(d, keys, default):
keys = keys.split(".") # You can avoid this first step if you're willing to use a list like ["1", "2", "3"...] as an input instead of a string like "1.2.3..."
for key in keys:
try:
d = d[key]
except KeyError:
return default
return d
如果你愿意,我可以把它描述一下 - 让我知道。请记住,除非您遇到或有理由相信您会遇到瓶颈,否则优化没有意义。