我想比较两个字典的键值对。进行比较的方式是,将第一字典的第一项与第二字典的第一项,第二项与第二项进行比较,并且这样进行。

词典的结构是:

dep_feats = {0: {'Gender': {'Masc'}, 'Number': {'Sing'}, 'Person': {'3'}}, 1: {}, 2: {'Number': {'Sing'}, 'Person': {'3'}}, 3: {'Gender': {'Masc'}, 'Number': {'Sing'}, 'Person': {'3'}, 'Tens': {'Past'}, 'Voice': {'Act'}}, 4: {}}




head_feats = {0: {'Gender': {'Masc'}, 'Number': {'Sing'}, 'Person': {'3'}, 'Tens': {'Past'}, 'Voice': {'Act'}}, 1: {'Number': {'Sing'}, 'Person': {'3'}}, 2: {'Gender': {'Masc'}, 'Number': {'Sing'}, 'Person': {'3'}, 'Tens': {'Past'}, 'Voice': {'Act'}}, 3: {}, 4: {'Gender': {'Masc'}, 'Number': {'Sing'}, 'Person': {'3'}, 'Tens': {'Past'}, 'Voice': {'Act'}}}


for i in range(len(dep_feats)):
    for j in range(len(head_feats)):
        if i == j:
            if dep_feats[i].items() == head_feats[i].items():
                print(dep_feats[i].items())

最佳答案

这可以用zip()完成

for dep, head in zip(dep_feats.items(), head_feats.items()):
    if dep == head:
        print(dep)

关于python - 比较在python中两个字典的相同索引处找到的键值对,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56993840/

10-12 02:07