如何迭代具有 n 大小 window 的列表并对匹配和不匹配的元素集进行操作?

How to iterate over a list with a n-sized window and operate on matched and unmatched set of elements?

我有两个元素列表,我想在 window 内进行匹配,而 fit/match 之外的内容则在 window 内进行匹配。例如,

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" # .... 

我应该能够从一个循环中调用 my_func(),例如,

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')
...

我想,我可以遍历 3 个元素-window

for i, (previ, currenti, nexti) in enumerate(zip(word_list, word_list[1:], word_list[2:])):
    find_match(reference_window, previ, currenti, nexti)

但这并不能解决我的问题。如果有任何帮助,我将不胜感激。

您可以为此使用正则表达式。正则表达式将采用 \b(a b c|p q r|b c d|\w)\b 形式,其中第一部分是根据潜在匹配列表构建的,然后默认为单个字符 \w(如果你想使用 \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

输出:

a
z
p
a b c
p
e
p q r