操作清单和字典清单

阿伦

我有一个列表列表,表示字典中的键。我希望为列表中的每个列表选择较小的键。例如,

L1 = [['1_A','2_A'],['1_B','2_B']]
D1 = {'1_A': 0.22876, '2_A': 0.22382, '1_B': 0.2584, '2_B': 0.25373}

for li in L1:
    for ll in li:
        if ll in D1.keys():
            print "Value for %s is %s" %(ll,D1[ll])
        else:
            print "Values not found"

当我打印它时,我得到:

Value for 1_A is 0.22876
Value for 2_A is 0.22382
Value for 1_B is 0.2584
Value for 2_B is 0.25373

我期望的输出是2_A2_B因为与1_A相比,它们两个都有较小的值1_B谁能建议该怎么做?

法汉

您没有在任何地方比较这些值。

L1 = [['1_A','2_A'],['1_B','2_B']]
D1 = {'1_A': 0.22876, '2_A': 0.22382, '1_B': 0.2584, '2_B': 0.25373}

template = "Value for {} is {}"

for i,j in L1:
    if D1[i] < D1[j]:
        print template.format(i,D1[i])
    else:
        print template.format(j,D1[j])

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章