<p>这段代码不是特别有效,但确实可以正常工作。也许有人会想出比使用<code>product</code>更好的方法来处理位置信息。同时,您可以使用此代码对其他算法进行测试。你知道吗</p>
<pre><code>from itertools import product
def words_range(src, query):
# Create a dict to store the word positions in src of each query word
pos = {s: [] for s in query}
for i, s in enumerate(src):
if s in pos:
pos[s].append(i)
print(pos)
# Find all the ranges that hold all the query word
ranges = ((min(t), max(t)) for t in product(*pos.values()))
# Find the smallest range
return min(ranges, key=lambda t:t[1] - t[0])
# Test
src = '''what about the lazy brown fox that jumped over the other
brown one which lazy dog ate the food of the fox'''.split()
for i, s in enumerate(src):
print(i, s)
query = 'lazy brown dog'.split()
print(words_range(src, query))
query = 'the lazy brown fox'.split()
print(words_range(src, query))
</code></pre>
<p><strong>输出</strong></p>
<pre><code>0 what
1 about
2 the
3 lazy
4 brown
5 fox
6 that
7 jumped
8 over
9 the
10 other
11 brown
12 one
13 which
14 lazy
15 dog
16 ate
17 the
18 food
19 of
20 the
21 fox
{'lazy': [3, 14], 'brown': [4, 11], 'dog': [15]}
(11, 15)
{'the': [2, 9, 17, 20], 'lazy': [3, 14], 'brown': [4, 11], 'fox': [5, 21]}
(2, 5)
</code></pre>