Python Forum
compare parts of a 2-tuple differently
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
compare parts of a 2-tuple differently
#1
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)
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List comprehension used differently coder_sw99 3 1,834 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  Two loops behaving differently DavidTheGrockle 5 2,613 Dec-27-2020, 03:56 AM
Last Post: deanhystad
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,962 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Hash command works differently for me in CMD and Spyder ZweiDCG 3 2,443 Sep-10-2019, 01:10 PM
Last Post: DeaD_EyE
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,335 May-21-2019, 11:39 AM
Last Post: avorane
  Why does Function behave differently in a class vs. outside a class? Leaf 4 3,755 Nov-30-2017, 11:13 AM
Last Post: DeaD_EyE
  looking for sweeter code to compare parts of a list Skaperen 7 5,018 Jun-20-2017, 12:16 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020