2017-10-16 85 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