比较Python中两个字典的值并从中构造新的字典

2024-09-28 03:20:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是python2.7.4。我尝试比较python中两个不同字典的值,并根据比较结果构建一个新字典。 我的用户将post positions以及马的mlodds1和tbodds1输入到3个列表中,然后我执行以下操作:

ml_dict = dict(zip(postpositions,mlodds1))
tb_dict = dict(zip(postpositions,tbodds1))

从这些列表中构造两个词典。 我想要一个新字典:screened_dict[a,x]由tb\u dict[a,x]和ml\u dict(a,y)中的值x<;y组成。提前谢谢。你知道吗


Tags: 用户lt列表字典zippostmltb
1条回答
网友
1楼 · 发布于 2024-09-28 03:20:57
combined = {}
for x in ml_dict:
    try:
        if tb_dict[x] < ml_dict[x]: combined[x] = ml_dict[x]
    except KeyError: continue

相关问题 更多 >

    热门问题