在嵌套 Python 字典中搜索键

发布于 2024-12-08 14:04:21 字数 463 浏览 2 评论 0原文

我有一些像这样的Python字典:

A = {id: {idnumber: condition},.... 

例如,

A = {1: {11 : 567.54}, 2: {14 : 123.13}, .....

我需要搜索字典是否有idnumber == 11并使用条件计算一些内容。但如果整个字典中没有任何 idnumber == 11,我需要继续使用下一个字典。

这是我的尝试:

for id, idnumber in A.iteritems():
    if 11 in idnumber.keys(): 
       calculate = ......
    else:
       break

I have some Python dictionaries like this:

A = {id: {idnumber: condition},.... 

e.g.

A = {1: {11 : 567.54}, 2: {14 : 123.13}, .....

I need to search if the dictionary has any idnumber == 11 and calculate something with the condition. But if in the entire dictionary doesn't have any idnumber == 11, I need to continue with the next dictionary.

This is my try:

for id, idnumber in A.iteritems():
    if 11 in idnumber.keys(): 
       calculate = ......
    else:
       break

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏花。依旧 2024-12-15 14:04:21

dpath 来救援。

http://github.com/akesterson/dpath-python

dpath 允许您按全局搜索,这会让你得到你想要的。

$ easy_install dpath
>>> for (path, value) in dpath.util.search(MY_DICT, '*/11', yielded=True):
>>> ... # 'value' will contain your condition; now do something with it.

它将迭代字典中的所有条件,因此不需要特殊的循环结构。

另请参阅

沐歌 2024-12-15 14:04:21

你很接近了。

idnum = 11
# The loop and 'if' are good
# You just had the 'break' in the wrong place
for id, idnumber in A.iteritems():
    if idnum in idnumber.keys(): # you can skip '.keys()', it's the default
       calculate = some_function_of(idnumber[idnum])
       break # if we find it we're done looking - leave the loop
    # otherwise we continue to the next dictionary
else:
    # this is the for loop's 'else' clause
    # if we don't find it at all, we end up here
    # because we never broke out of the loop
    calculate = your_default_value
    # or whatever you want to do if you don't find it

如果您需要知道内部 dict 中有多少个 11 作为键,您可以:

idnum = 11
print sum(idnum in idnumber for idnumber in A.itervalues())

这有效,因为键只能在每个 dict 中 一次,因此您只需测试该键是否存在即可。 in 返回 TrueFalse,它们等于 10,因此sumidnum 出现的次数。

You're close.

idnum = 11
# The loop and 'if' are good
# You just had the 'break' in the wrong place
for id, idnumber in A.iteritems():
    if idnum in idnumber.keys(): # you can skip '.keys()', it's the default
       calculate = some_function_of(idnumber[idnum])
       break # if we find it we're done looking - leave the loop
    # otherwise we continue to the next dictionary
else:
    # this is the for loop's 'else' clause
    # if we don't find it at all, we end up here
    # because we never broke out of the loop
    calculate = your_default_value
    # or whatever you want to do if you don't find it

If you need to know how many 11s there are as keys in the inner dicts, you can:

idnum = 11
print sum(idnum in idnumber for idnumber in A.itervalues())

This works because a key can only be in each dict once so you just have to test if the key exits. in returns True or False which are equal to 1 and 0, so the sum is the number of occurences of idnum.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文