2017-10-16 86 views
0

我有以下“词典”:的Python:基于字典逻辑comparater值

a = {"one":(10, 11.25), "two":(11, 12.25), "three":(12, 13.25)} 

我想建立基于关于每一对(即,在该例子中的第一个值的“if语句”我比较10,11,12)。

我该怎么做呢?

我一直在玩'a.values()'函数,但列出了所有的值。我想遍历每个值并验证它们是否正面。

谢谢!

回答

4

可以使用all有发电机:

if all(v[0] > 0 for v in a.values()): 
0

小和更容易一些解决方案

a = {"one":(10, 11.25), "two":(11, 12.25), "three":(-12, 13.25)} 

for x,y in a.iteritems(): 
    print "Number is ",y[0] 
    if y[0] > 0: 
     print "Positive" 
    else: 
     print "Negative" 

输出:

Number is -12 
Negative 
Number is 11 
Positive 
Number is 10 
Positive