【问题标题】:Comparing Values of 2 dictionaries in Python and Constructing New dictionary From Comparison在 Python 中比较 2 个字典的值并通过比较构造新字典
【发布时间】:2013-05-20 14:31:33
【问题描述】:

我使用的是 Python 2.7.4。我正在尝试比较 python 中两个不同字典的值,并根据比较结果构建一个新字典。 我的用户将马的帖子位置和 mlodds1 和 tbodds1 输入到 3 个列表中,然后我执行以下操作:

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

从这些列表中构造两个字典。 我想要一个新字典:screened_dict[a,x] 由 tb_dict[a,x] 和 ml_dict(a, y) 中的值 x

【问题讨论】:

  • 我很难理解你在这里想要什么。您能否提供一个简单的ml_dicttb_dict 示例,然后您希望screened_dict 是什么?
  • 当然。 postpositions = (1,2,3...8),早盘赔率由专业让分盘设定。这些赔率 mlodds1 由用户在我的程序中作为数字输入到列表 mlodds1 (2.5,4,6,8,10,15,20,25) 中,表示赔率 5/2、4/1 等每个马柱位置。托特板赔率 tbodds1 作为轨道上托特板的实际赔率列表输入,它可能是一个类似 (3,2,6,8,9,15,20,30) 的列表,然后字典是 ml_dict = {1 :2.5,2:4,3:6,4:8,5:10,6:15,7:20,8:25} 和 tb_dict = {1:3,2:2,3:6,4:8 ,5:9,6:15,7:20,8:30} 然后 screened_dict = {2:2,5:9,} 在这个特定的例子中。

标签: python dictionary compare iteration


【解决方案1】:
combined = {}
for x in ml_dict:
    try:
        if tb_dict[x] < ml_dict[x]: combined[x] = ml_dict[x]
    except KeyError: continue

【讨论】:

  • 我是这样实现的:for x in ml_dict: try: if tb_dict[x]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-09
  • 2019-06-15
相关资源
最近更新 更多