I've got a list of different strings (this is an example):
strs = ["FOOBAR", "PYTHON", "MAPARTS"]
and I've got another list with substrings that one of the strings in the previous list might contain:
substrs = ["ARTS", "FOO", "PY", "BAR", "MAP"]
I want to make a list that has all the strings in strs
that can be split using two strings in substrs
, split by them and wrapped in a list or tuple. So the finished list would look like:
[("FOO", "BAR"), ("MAP", "ARTS")]
I can't wrap my head around how to manage it, at least in a simple way. Any help?
Here is an interesting approach if you want to check if any combination of two tokens
is a word listed in words
:
from itertools import product
words = ["FOOBAR", "PYTHON", "MAPARTS"]
tokens = ["ARTS", "FOO", "PY", "BAR", "MAP"]
pairs = [_ for _ in product(tokens, tokens) if ''.join(_) in words]
Resulting in:
>>> pairs
[('FOO', 'BAR'), ('MAP', 'ARTS')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With