gpt4 book ai didi

python - 比较在Python中两个字典的相同索引处找到的键值对

转载 作者:太空宇宙 更新时间:2023-11-03 20:38:46 25 4
gpt4 key购买 nike

我想比较两个字典的键值对。比较的方式是,将第一个字典的第一项与第二个字典的第一项进行比较,将第二项与第二项进行比较,如下所示。

字典的结构是:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com