擅长:python、mysql、java
<p>我不能评论(我没有足够的声誉),所以这个答案在技术上不是一个答案。在</p>
<p>我不太熟悉regex,但是假设您的<code>re.findall()</code>成功,您可以使用以下代码:</p>
<pre><code>import re, itertools
from collections import Counter
f = open('C:\\Python27\\test\\A.txt')
text = f.read()
everything = []
define_words = ['contractual', 'obligation', 'law', 'employer']
for k in define_words:
everything.append(re.findall(r"([^.]*?%s[^.]*\.)" % k,text))
everything = list(itertools.chain(*everything))
counts = Counter(everything)
everything = [value for value, count in counts.items() if count > 1]
everything = list(itertools.chain(*everything))
print everything
</code></pre>
<p>这将循环遍历数组列表并将值添加到列表中,从而生成列表列表。然后我只保留重复项(好值),并将列表列表转换为一个列表。在</p>
<p><strong>错误:</strong>真正的错误是所有东西都是一个列表列表,<code>Counter(everything)</code>不允许这样做。因此,我在<code>Counter()</code>之前将其剥离。在</p>