Python Forum

Full Version: compare parts of a 2-tuple differently
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have a function that returns a 2-tuple. i have a previous value from that same function i want to compare it with. the previous value is an item from a dictionary. the [1] item of the tuple is a value that is always an int. the [0] value of the tuple is some arbitrary value of some arbitrary type that can be compared with an ==. i want to test both values this way. if the [0] values are different in any way, the result will be False. in another case the result will be True. if the [0] vales are exactly the same, then the [1] values (always type int) are compared with either < or with > forming the result.

the above seems easy enough to do in 3 or 4 lines of code. i'm trying to think up how to get the result in just 1 line. my intention is to iterate a list of indexes being passed to the function and indexing the dictionary and give this list to sum() to get a count of how many of these have a result of True. has anyone coded comparison of 2-tuples with differences like this nice and tightly? or should i stay with the 3 to 4 line case for readability. sometimes fewer lines helps readability of the broader logic. in this case it avoids spreading sum() around 4 lines.

some code i dreamed up:
count = 0
for x in foo:
    t1 = tupler(x)
    t2 = tuples[x]
    if t1[0] == t2[0]:
        if t1[1] > t2[1]:
            count += 1
print(count)