Python正则表达式-检查重复单词
我如何检查以下句子是否有重复的单词并识别它们是什么? “樱桃树将在四月底开花” 我用过: 进口稀土 check=re.search(r'(\b\w+\s+\1',“多伦多高公园的樱桃树将于3月下旬开花”) 打印支票组(1)Python正则表达式-检查重复单词,python,Python,我如何检查以下句子是否有重复的单词并识别它们是什么? “樱桃树将在四月底开花” 我用过: 进口稀土 check=re.search(r'(\b\w+\s+\1',“多伦多高公园的樱桃树将于3月下旬开花”) 打印支票组(1) 它只会给我“绽放”,而不会给我“在” 像这样的方法会奏效: >>> text = 'Cherry tree blooming blooming will begin in in late April'.split() >>> any(i
它只会给我“绽放”,而不会给我“在” 像这样的方法会奏效:
>>> text = 'Cherry tree blooming blooming will begin in in late April'.split()
>>> any(i for i in text if text.count(i) > 1)
True
使用集合。计数器
>>> my_str = 'Cherry tree blooming blooming will begin in in late April'
>>> import collections
>>> collections.Counter(my_str.split()) # splits the string on whitespace and counts the word occurance.
Counter({'blooming': 2, 'in': 2, 'late': 1, 'begin': 1, 'Cherry': 1, 'tree': 1, 'will': 1, 'April': 1})
>>> any(x for x in collections.Counter(my_str.split()).values() if x>1)
True
定义并使用如下函数:
def check_dup(sentence):
words = sentence.split(" ")
dup = {}
for word in words:
if word in dup.keys():
return True
dup[word] = "present"
return False
print check_dup(sentence)
可以使用集合来跟踪您以前看到的单词:
sentence = 'Cherry tree blooming blooming will begin in in late April'
seen = set()
for word in sentence.split(' '):
if word in seen:
print(word)
seen.add(word)
您能否展示您当前的编码尝试,并解释当前不起作用的内容?