比较嵌套字典中的值

时间:2017-10-13 08:10:39

标签: python dictionary nested

我有非常简单的2阶段字典的问题。 我的字典中包含键(数字),第二个键(数字)和名称公司的值。 我只需要检查1号键,因为我知道只有“FORD”名称出现的可能性 - 是字典[i] [1] 正如您所看到的 - 字符串在100%与密钥不相等。键可以包含“福特”(但键中有另一个部分名称:“福特马达”),所以如果“福特”出现在每个词典[i] [1]中 - 按顺序将此键分配给空词典“MATCHINGITEMS”:

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"

1 个答案:

答案 0 :(得分:2)

如果我理解正确,那么你可以做到:

 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)