Python Forum
Check common element in tuples
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check common element in tuples
#4
Quote:Ts = [(45,50,30,21), (30,9,56,3), (56,10,17,4), (41,30,50,56)]
The third tuple starts with 56 which also appears in the second tuple. If that shouldn't result in setting result[2] = True then I don't understand your tuple matching rule.

You may not understand "result" in the first example because there is an error. I messed up what True and False meant:
def find_matches(tuples):
    result = [False]  # <- Was incorrectly set to True
    for i in range(len(tuples)-1):
        pair = list(set(tuples[i])) + list(set(tuples[i+1]))  // Protect against duplicates in a list
        result.append(len(set(pair)) != len(pair))  # And corresponding error here.
    return result
                    
print(find_matches([(45,50,30,21), (30,9,56,3), (57,10,21,4), (41,30,50,56)]))
What this program does is grab two consecutive tuples and add them together. First time through it creates a list from the first two tuples: (45,50,30,21) and (30,9,56,3) and calls this pair.

pair = [45,50,30,21,30,9,56,3]

Then it compares the length of pair to the lenght of set(pair). Creating a set from a list removes any duplicate values, so the only way len(pair) == len(set(pair)) is if all values in pair are unique.

pair = [45,50,30,21,30,9,56,3], set(pair) = {3,9,45,50,21,56,30}. len(pair) != len(set(pair))

I ran the code on a few different test cases and noticed it failed for tuples like this:

Ts = [(1,1,1,1), (2,2,2,2), (3,3,3,3), 4,4,4,4)]

The result for this should be [False, False, False, False] but it came up [False, True, True, True]. The reason is that set(tuple) removes all duplicates, so (1,1,1,1,2,2,2,2) becomes (1,2). To guard against this I need to remove all duplicates from each tuple before combining them in pair.
pair = list(set((1,1,1,1)) + list(set((2,2,2,2)) == [1] + [2] == [1, 2]
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,294 Jun-14-2022, 08:35 PM
Last Post: thesquid
  check if element is in a list in a dictionary value ambrozote 4 2,065 May-11-2022, 06:05 PM
Last Post: deanhystad
  How to check if my argument is a tuple with 3 tuples zarox 1 1,866 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,179 Nov-13-2017, 07:05 PM
Last Post: Larz60+
  common elements of a list Skaperen 5 9,496 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,433 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