Find subsequences of strings within strings

2024/9/8 10:37:02

I want to make a function which checks a string for occurrences of other strings within them.
However, the sub-strings which are being checked may be interrupted within the main string by other letters.

For instance:

a = 'abcde'
b = 'ace'
c = 'acb'

The function in question should return as b being in a, but not c.

I've tried set(a). intersection(set(b)) already, and my problem with that is that it returns c as being in a.

Answer

You can turn your expected sequence into a regex:

import redef sequence_in(s1, s2):"""Does `s1` appear in sequence in `s2`?"""pat = ".*".join(s1)if re.search(pat, s2):return Truereturn False# or, more compactly:
def sequence_in(s1, s2):"""Does `s1` appear in sequence in `s2`?"""return bool(re.search(".*".join(s1), s2))a = 'abcde' 
b = 'ace' 
c = 'acb'assert sequence_in(b, a)
assert not sequence_in(c, a)

"ace" gets turned into the regex "a.*c.*e", which finds those three characters in sequence, with possible intervening characters.

https://en.xdnf.cn/q/73167.html

Related Q&A

How to bestow string-ness on my class?

I want a string with one additional attribute, lets say whether to print it in red or green.Subclassing(str) does not work, as it is immutable. I see the value, but it can be annoying.Can multiple inhe…

How to pass Python instance to C++ via Python/C API

Im extending my library with Python (2.7) by wrapping interfaces with SWIG 2.0, and have a graph object in which I want to create a visitor. In C++, the interface looks like this:struct Visitor{virtua…

REST API in Python with FastAPI and pydantic: read-only property in model

Assume a REST API which defines a POST method on a resource /foos to create a new Foo. When creating a Foo the name of the Foo is an input parameter (present in the request body). When the server creat…

a class with all static methods [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

How can I find null values with SELECT query in psycopg?

I am using psycopg2 library in python and the INSERT query works good when I insert null Value with None, but when I want to do SELECT null values, with None doesnt return any.cur.execute("SELECT …

Pause and continue stopwatch

I am trying to create stopwatch. I have done it but I would like to pause and continue the time whenever I want. I have tried some things but I have no idea how to do it. Is there anybody who would exp…

How do I escape `@` letter from SQL password in connection URI [duplicate]

This question already has an answer here:handle @ in mongodb connection string(1 answer)Closed 9 years ago.when you connect to mongodb using python from SQLAlchamey, we use mongodb://username:password@…

Set WTForms submit button to icon

I want a submit button that displays an icon rather than text. The button is a field in a WTForms form. I am using Bootstrap and Open Iconic for styling and icons. How do I set the submit field to d…

what is the significance of `__repr__` function over normal function [duplicate]

This question already has answers here:Purpose of __repr__ method?(6 answers)Closed 5 years ago.I am trying to learn python with my own and i stucked at __repr__ function. Though i have read lots of p…

Using celery with Flask app context gives Popped wrong app context. AssertionError

Im more or less using the setup to run Celery tasks using your flask app context from here: http://flask.pocoo.org/docs/0.10/patterns/celery/Im getting the same error message as Create, manage and kill…