首页 > 解决方案 > 在字典中检查字典中python中的特定值

问题描述

我正在使用由多个字典组成的字典:

my_dictt = {0: {'n': 1, 's': 5, 'w': 7, 'e': 3}, 7: {'w': 8, 'e': 0}, 8: {'e': 7}, 1: {'n': 2, 's': 0}, 2: {'s': 1}, 5: {'n': 0, 's': 6}, 6: {'n': 5}, 3: {'w': 0, 'e': 4}, 4: {'w': 3}}

我需要检查任何嵌套值中的任何值是否为“?”,但无法这样做。我尝试遍历字典并使用 dict.values() 函数,但到目前为止还没有让它工作。有谁知道我怎么能做到这一点?

标签: pythondictionary

解决方案


我不知道这是否正是您正在寻找的。在评论中让我知道。

my_dictt = {0: {'n': 1, 's': 5, 'w': 7, 'e': 3}, 7: {'w': 8, 'e': 0}, 8: {'e': 7}, 1: {'n': 2, 's': 0}, 2: {'s': 1}, 5: {'n': 0, 's': 6}, 6: {'n': 5}, 3: {'w': 0, 'e': 4}, 4: {'w': 3}}
#first go to each value of yor dictionary
for eachValue in my_dictt.values():
    #then in each value that are dictionaries get each value again
    for eachNestedValue in eachValue.values():
        # check if any of values are ?
        if eachNestedValue == "?":
            print(True)

推荐阅读