I have two list of elements, and I want to match within a window, and things that does not fit/match into a window. For example,
reference_window_list = ["a b c", "p q r", "b c d"]
input_elements = "a z p a b c p e p q r" # ....
I should be able to call my_func() from a loop like,
my_func('a') // First element of the list
my_func('z') // Second element of the list
my_func('p') // Third element of the list
my_func('a b c') // Forth group of elements that matched with reference_window
my_func('p')
....
my_func('p q r')
...
I thought, I could loop through 3 elements-window
for i, (previ, currenti, nexti) in enumerate(zip(word_list, word_list[1:], word_list[2:])):
find_match(reference_window, previ, currenti, nexti)
But that does NOT solve my problem. I would appreciate any help.
CodePudding user response:
You can use a regex for that. The regex would be in the form \b(a b c|p q r|b c d|\w)\b
where the first part is build from the list of potential matches and then defaults to a single character \w
(or more if you want using \w
)
import re
reference_window_list = ["a b c", "p q r", "b c d"]
input_elements = "a z p a b c p e p q r"
reg = r'\b(%s|\w)\b' % '|'.join(reference_window_list)
for e in re.finditer(reg, input_elements):
print(e.group()) # used print here as dummy function
output:
a
z
p
a b c
p
e
p q r