Python Forum
Check common element in tuples
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check common element in tuples
#6
You have 4 tuples, so actually only 3 comparisons to the previous tuple can be made. I took a stab in the code below. I gave a pass "True" to the first tuple, then for each subsequent tuple compared each element to see if in the prior tuple. If so, print True, if not continue until have checked all the elements in the tuple at which point print False. See if this works for you.

def find_tuple_matches(tuples) :
    for count, mississippi in enumerate(tuples) :
        if count == 0:
            print('False')
            continue
        flag = False
        for tupelo in mississippi :
            if tupelo in tuples[count-1] :
                print('True')
                flag = True
                break
            
        if not flag :
            print('False')

find_tuple_matches([(45,50,30,21), (30,9,56,3), (57,10,21,4), (41,30,50,56)])
Reply


Messages In This Thread
Check common element in tuples - by PUP280 - Nov-01-2020, 03:42 PM
RE: Check common element in tuples - by deanhystad - Nov-01-2020, 04:14 PM
RE: Check common element in tuples - by PUP280 - Nov-01-2020, 04:55 PM
RE: Check common element in tuples - by deanhystad - Nov-01-2020, 05:32 PM
RE: Check common element in tuples - by deanhystad - Nov-01-2020, 05:54 PM
RE: Check common element in tuples - by jefsummers - Nov-01-2020, 07:55 PM
RE: Check common element in tuples - by deanhystad - Nov-01-2020, 09:51 PM
RE: Check common element in tuples - by perfringo - Nov-02-2020, 09:18 AM
RE: Check common element in tuples - by deanhystad - Nov-02-2020, 07:05 PM
RE: Check common element in tuples - by volcano63 - Nov-02-2020, 07:25 PM
RE: Check common element in tuples - by PUP280 - Nov-05-2020, 05:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 1,293 Jun-14-2022, 08:35 PM
Last Post: thesquid
  check if element is in a list in a dictionary value ambrozote 4 2,060 May-11-2022, 06:05 PM
Last Post: deanhystad
  How to check if my argument is a tuple with 3 tuples zarox 1 1,864 Nov-15-2020, 06:50 PM
Last Post: DougBTX
  Unable to locate element no such element gahhon 6 4,570 Feb-18-2019, 02:09 PM
Last Post: gahhon
  Change single element in 2D list changes every 1D element AceScottie 9 12,175 Nov-13-2017, 07:05 PM
Last Post: Larz60+
  common elements of a list Skaperen 5 9,491 Mar-22-2017, 10:13 AM
Last Post: buran
  Check to see if a string exactly matches an element in a list DBS 1 22,427 Nov-02-2016, 10:16 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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