[英]How to find whether a string either a key or a value in a dict or in a dict of dicts
这看起来应该很简单,但由于某种原因我无法到达那里。
我有几种可能的格式的字典:
dict1 = {"repo": "val1", "org":"val2"}
dict2 = {"key1":"repo", "key2":"org"}
dict3 = {"key1":"org and stuff"}
dict4 = {"org and stuff":"value1"}
dict5 = {"key1":{"value1":"value2"}, "key2":{"1":"org"}}
dict6 = {"org":{"value1":"value2"}, "key2":{"value1":"value2"}}
dict7 = {"org":{"subkey1":{"value1":"value2"}}, "key2":{"value1":"value2"}}
dict8 = {"key1":{"subkey1":{"value1":"org"}}, "key2":{"value1":"value2"}}
我想搜索字符串'org'
,如果它在字典中的任何位置(键、值、键 [子键] [值] 等),则返回 true。 我不想要部分字符串匹配。
也就是说,我正在寻找以下结果:
True
True
False
False
True
True
True
True
我已经阅读了这些问题,但没有一个能完全回答,因为我可以嵌套字典:
如何从所有 Dictionary.Values() 中的字符串中搜索所有字符
使用递归!
def indict(thedict, thestring):
if thestring in thedict:
return True
for val in thedict.values():
if isinstance(val, dict) and indict(val, thestring):
return True
elif isinstance(val, str) and val == thestring:
return True
return False
从技术上讲,您可以将dict转换为字符串,然后使用re
搜索该单词是否存在。 另外,如果您的决定非常大,则不需要循环,并且比使用循环/递归要快/简单。 另外,如果您有子列表或元组等
import re
def indict(data, word):
return bool(re.search(f"'{word}'", str(data)))
为了进行概念验证,请使用我测试过的命令,这将返回您想要的结果。
dicts = (dict1, dict2, dict3, dict4, dict5, dict6, dict7, dict8)
for d in dicts:
print(indict(d, 'org'))
打印:
True
True
False
False
True
True
True
True
True
好像adrtam首先到达这里,但我基本上想到了同一件事
def searcher(input_dict, search_item):
for key, value in input_dict.items():
if search_item == key or search_item == value:
return True
elif type(value) is dict:
searcher(input_dict[key], search_item) #search recursively
return False
递归是一个不错的选择。 遍历字典的键和值,寻找目标字符串并递归我们一路上找到的任何嵌套字典值:
def in_nested_dict(needle, haystack):
if isinstance(haystack, dict):
return any(
k == needle or v == needle or in_nested_dict(needle, v)
for k, v in haystack.items()
)
return False
if __name__ == "__main__":
tests = [
{"repo": "val1", "org": "val2"},
{"key1": "repo", "key2": "org"},
{"key1": "org and stuff"},
{"org and stuff": "value1"},
{"key1": {"value1": "value2"}, "key2": {"1": "org"}},
{"org": {"value1": "value2"}, "key2": {"value1": "value2"}},
{"org": {"subkey1": {"value1": "value2"}}, "key2": {"value1": "value2"}},
{"key1": {"subkey1": {"value1": "org"}}, "key2": {"value1": "value2"}},
]
for test in tests:
print(in_nested_dict("org", test))
Output:
True
True
False
False
True
True
True
True
如果搜索字符串在给定字典的任何键或值中,或者如果给定字典实际上不是字典,则可以使用递归函数返回True
,如果搜索字符串等于它,则返回:
def is_in(d, s):
return s in d or any(is_in(v, s) for v in d.values()) if isinstance(d, dict) else d == s
以便:
for d in [
{"repo": "val1", "org":"val2"},
{"key1":"repo", "key2":"org"},
{"key1":"org and stuff"},
{"org and stuff":"value1"},
{"key1":{"value1":"value2"}, "key2":{"1":"org"}},
{"org":{"value1":"value2"}, "key2":{"value1":"value2"}},
{"org":{"subkey1":{"value1":"value2"}}, "key2":{"value1":"value2"}},
{"key1":{"subkey1":{"value1":"org"}}, "key2":{"value1":"value2"}}]:
print(is_in(d, 'org'))
输出:
True
True
False
False
True
True
True
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.