Python Forum
logic comparater based on dictionary values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: logic comparater based on dictionary values (/thread-5679.html)



logic comparater based on dictionary values - ijosefson - Oct-16-2017

I have the following 'dictionary':
a = {"one":(10, 11.25), "two":(11, 12.25), "three":(12, 13.25)}

I would like to build an 'if statement' based on on the first value of each pair (i.e. in this example I'm comparing 10, 11, 12).

How would I go about doing so?

I've been playing around with the 'a.values()' function but that lists all the values. I would like to iterate through each value and verify they are positive.

Thanks!


RE: logic comparater based on dictionary values - Larz60+ - Oct-16-2017

show what you've tried so far.


RE: logic comparater based on dictionary values - ijosefson - Oct-16-2017

if __name__ == "__main__":
    a = {"10-01-2014":(10, 11.25), "10-02-2014":(11, 12.25), "10-03-2014":(12, 13.25)}
    if (v1 in a()) < 0:
        print('Value is negative!')
    else:
        print('Value is good')



RE: logic comparater based on dictionary values - Mekire - Oct-16-2017

(Oct-16-2017, 03:56 AM)ijosefson Wrote: I would like to iterate through each value and verify they are positive.
Well, you are going to need to try iterating then:
some_dict = {"a" : 5, "b" : 7, "c" : 9}


for key in some_dict:
    print(key)

for val in some_dict.values():
    print(val)

for key,val in some_dict.items():
    print(key, val)