假设 的长度b
为 3,即使a
contains'k','h','p'
使得它们并不总是正确地相互跟随,以下代码也将起作用,如下所示:
a=[('test', 'k'), ('jamy', 'k'), ('Park', 'h'), ('kick', 'p'), ('car', 'k'),
('an', 'r'),('rock', 'h'), ('see','k'), ('it','h'),('an', 'o'),('works', 'p')]
b = ['k','h','p']
将产生:
[('jamy', 'Park', 'kick'), ('see', 'it', 'works')]
代码:
letters_a = "".join(str(tup[1]) for tup in a)
letters_b = "".join(str(letter) for letter in b)
regex = re.compile(r'(%s)[^%s]*(%s)[^%s]*(%s)'
% (letters_b[0],letters_b[:2],letters_b[1],letters_b,letters_b[2]))
#for this example, the above line translates to:
#regex = re.compile(r'(k)[^kh]*(h)[^khp]*(p)')
match = re.finditer(regex, letters_a)
results=[]
for m in match:
first,second,third = m.start(1), m.start(2), m.start(3)
results.append((a[first][0],a[second][0],a[third][0]))
print results