Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 3.x 匹配嵌套字典中的项并返回外键_Python 3.x_Dictionary_Nested_Match - Fatal编程技术网

Python 3.x 匹配嵌套字典中的项并返回外键

Python 3.x 匹配嵌套字典中的项并返回外键,python-3.x,dictionary,nested,match,Python 3.x,Dictionary,Nested,Match,您好,我正在尝试编写一些代码来比较多个嵌套字典,以获得特定项的匹配,如果为true,则返回外键 我曾尝试使用嵌套for循环和字典名称列表来排序和查找匹配项,代码首先获取单独的字典dict1和dict2,然后比较每个字典内的['ID']键以查找匹配项,见下文: Dictionary1 = {'key1':{'ID':'ID1'}, 'key2':{'ID':'ID2'}, 'key3':{'ID':'ID3'}} Dictionary2 = {'key4':{'ID':'ID1'}, 'key5

您好,我正在尝试编写一些代码来比较多个嵌套字典,以获得特定项的匹配,如果为true,则返回外键

我曾尝试使用嵌套for循环和字典名称列表来排序和查找匹配项,代码首先获取单独的字典dict1和dict2,然后比较每个字典内的['ID']键以查找匹配项,见下文:

Dictionary1 = {'key1':{'ID':'ID1'}, 'key2':{'ID':'ID2'}, 'key3':{'ID':'ID3'}}

Dictionary2 = {'key4':{'ID':'ID1'}, 'key5':{'ID':'ID2'}, 'key6':{'ID':'ID3'}}



DictionaryList = [Dictionary1, Dictionary2]


for Dict1 in DictionaryList:
    for Dict2 in DictionaryList:
        for Key1 in Dict1:
            for Key2 in Dict2:
                if Dict1[Key1]['ID'] == Dict2[Key2]['ID']:
                    print('Dictionary1' + ': ' + Key1 + ' // ' 'Dictionary2' + ': ' + Key2)

此代码返回12个匹配项及其外键,其中大多数是错误的,请参见以下内容:

Dictionary1: key1 // Dictionary2: key1
Dictionary1: key2 // Dictionary2: key2
Dictionary1: key3 // Dictionary2: key3
Dictionary1: key1 // Dictionary2: key4
Dictionary1: key2 // Dictionary2: key5
Dictionary1: key3 // Dictionary2: key6
Dictionary1: key4 // Dictionary2: key1
Dictionary1: key5 // Dictionary2: key2
Dictionary1: key6 // Dictionary2: key3
Dictionary1: key4 // Dictionary2: key4
Dictionary1: key5 // Dictionary2: key5
Dictionary1: key6 // Dictionary2: key6

我怀疑这是因为字典本身和彼此之间进行了多次匹配,即代码将字典1与字典1进行比较,然后将字典2与字典2进行比较,以此类推

预期结果将是打印3个匹配项的代码:

Dictionary1: key1 // Dictionary2: key4
Dictionary1: key2 // Dictionary2: key5
Dictionary1: key3 // Dictionary2: key6


我是python/编程新手,因此非常感谢您提供的任何帮助。

我不知道您为什么将这些词典合并到一个列表中,然后尝试对其进行迭代。您可以简单地使用字典本身:

for Key1 in Dictionary1:
    for Key2 in Dictionary2:
        if Dictionary1[Key1]['ID'] == Dictionary2[Key2]['ID']:
            print('Dictionary1' + ': ' + Key1 + ' // ' 'Dictionary2' + ': ' + Key2)
这会打印出来

Dictionary1: key1 // Dictionary2: key4
Dictionary1: key2 // Dictionary2: key5
Dictionary1: key3 // Dictionary2: key6

我不知道你为什么把这些词典合并成一个列表,然后试着迭代。您可以简单地使用字典本身:

for Key1 in Dictionary1:
    for Key2 in Dictionary2:
        if Dictionary1[Key1]['ID'] == Dictionary2[Key2]['ID']:
            print('Dictionary1' + ': ' + Key1 + ' // ' 'Dictionary2' + ': ' + Key2)
这会打印出来

Dictionary1: key1 // Dictionary2: key4
Dictionary1: key2 // Dictionary2: key5
Dictionary1: key3 // Dictionary2: key6

您可以创建反向映射dict,将
Dictionary2
中的ID映射到它们的键,这样您就可以通过迭代
Dictionary1
在线性时间内获得所需的输出,并通过反向映射dict将每个ID映射到
Dictionary2
中的相应键:

mapping = {d['ID']: k for k, d in Dictionary2.items()}
for k, d in Dictionary1.items():
    print('Dictionary1' + ': ' + k + ' // ' 'Dictionary2' + ': ' + mapping[d['ID']])
这将产生:

Dictionary1: key1 // Dictionary2: key4
Dictionary1: key2 // Dictionary2: key5
Dictionary1: key3 // Dictionary2: key6

您可以创建反向映射dict,将
Dictionary2
中的ID映射到它们的键,这样您就可以通过迭代
Dictionary1
在线性时间内获得所需的输出,并通过反向映射dict将每个ID映射到
Dictionary2
中的相应键:

mapping = {d['ID']: k for k, d in Dictionary2.items()}
for k, d in Dictionary1.items():
    print('Dictionary1' + ': ' + k + ' // ' 'Dictionary2' + ': ' + mapping[d['ID']])
这将产生:

Dictionary1: key1 // Dictionary2: key4
Dictionary1: key2 // Dictionary2: key5
Dictionary1: key3 // Dictionary2: key6

嗨,我实际上有12本字典我想匹配其中的项目,我只用了两本作为例子。因此,我认为创建一个列表并对其进行迭代比为字典1到12中的键1编写字典1到12中的键2更容易。在本例中,使用
itertools.combines()
生成所有可能的双字典组合。嗨,我实际上有12个字典要匹配其中的项,我只举了两个例子。因此,我认为创建一个列表并对其进行迭代比为Dictionary 1到12中的key1编写Dictionary 1到12中的key2更容易生成所有可能的双字典组合。抱歉,我应该澄清,尽管在我的示例中,我的实际问题中只有两个字典,但我正在尝试匹配12个单独字典中的项目。抱歉,我应该澄清,尽管在我的示例中,我的实际问题中只有两个字典,但我正在尝试匹配12个单独字典中的项目个人词典。