-2

I am trying to check if shorter tuples are contained inside a longer one, but I need the order in which things are placed in the larger tuple to be respected during the checking. Here is an example:

A = ('A', 'B', 'C')
B = ('A', 'B')
C = ('A', 'C')
set(B).issubset(A)
set(C).issubset(A)

My expectation is set(B).issubset(A) to return True (as it is), but set(C).issubset(A) should return False. Is there a way to do this without a tedious for loops?

3
  • 4
    Not using sets, as they explicitely are unordered. Commented Feb 14, 2022 at 15:42
  • 4
    Using set.issubset in an order-aware way is a red-herring. You don't want to look for subsets, but apparently for subsequences. Commented Feb 14, 2022 at 15:44
  • You need to use lists or tuples for order to be retained. Commented Feb 14, 2022 at 15:44

1 Answer 1

0

as stated in the comments: this makes no sense as "sets'.

However, if all your elements are strings, the behavior of the "in" operator for strings may be enough for you. In order for it work you can join your tuples in a single string and then check with "in":

"".join(B) in "".join(A)

will yield your desired outputs. The drawback is if your strings can give you false positives, for example in:

A = ("AB", "C")
B = ("B", "C")

will give you a match - if that is a possibility, even a remote one, you will have to implement a manual search with a for loop and some state variables.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.