Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 比较嵌套字典中的值_Python_Dictionary_Nested - Fatal编程技术网

Python 比较嵌套字典中的值

Python 比较嵌套字典中的值,python,dictionary,nested,Python,Dictionary,Nested,我对非常简单的两阶段字典有问题。 我的字典有数字键、第二个数字键和公司名称。 我只需要检查1号键,因为我知道福特名字出现的唯一可能性是字典[I][1] 正如您所看到的,字符串与键在100%上是不相等的。密钥可以包含Ford,但密钥中有另一个零件名称:Ford Motor,因此如果Ford出现在每个字典中[i][1]-请按顺序将此密钥分配给空字典匹配项: matchingitems[1] = "Ford" matchingitems[2] = "Ford" etc 你能帮我吗 Dictionar

我对非常简单的两阶段字典有问题。 我的字典有数字键、第二个数字键和公司名称。 我只需要检查1号键,因为我知道福特名字出现的唯一可能性是字典[I][1] 正如您所看到的,字符串与键在100%上是不相等的。密钥可以包含Ford,但密钥中有另一个零件名称:Ford Motor,因此如果Ford出现在每个字典中[i][1]-请按顺序将此密钥分配给空字典匹配项:

matchingitems[1] = "Ford"
matchingitems[2] = "Ford" etc
你能帮我吗

Dictionary = { „1” : { „1” : "Ford Motor",
                   „2” : "Volkswagen Autos"
                      }
               "2" : { "1" : "Ducati",
                       "2" : "Yamaha"
                      } 
              "3" : { "1" : "Ford",
                      "2" : "SEAT"
                    }
               }
matchingitems = {}
i = 0
for value in Dictionary.items():
    for key in Dictionary.items[value]:
        i += 1
        if "Ford" in Dictionary[i][1]:
             matchingitems[i] = "Ford"

如果我理解正确,这正是您所需要的:

 Dictionary = { "1" : { "1" : "Ford Motor",
                   "2" : "Volkswagen Autos"
                      },
               "2" : { "1" : "Ducati",
                       "2" : "Yamaha"
                      },
              "3" : { "1" : "Ford",
                      "2" : "SEAT"
                    }
               }
matchingitems = {}

for key,value in Dictionary.items():
    if 'Ford' in value['1']:
        matchingitems[key] = value['1']

print(matchingitems)

我不会是随机的,因为dict可能不是一个有序dict,因此枚举的索引可能会改变吗?是的,你是对的。我相应地修改了代码。这也不是OP想要的。而且,键可能会增加超过1,我想你需要匹配项。appendvalue['1']请重新加载我的答案,我同时更改了它:-我给你+1作为持久性!