1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > python使用正则表达式去除句子中的重复词

python使用正则表达式去除句子中的重复词

时间:2021-06-21 05:48:55

相关推荐

python使用正则表达式去除句子中的重复词

python使用正则表达式去除句子中的重复词

#python使用正则表达式去除句子中的重复词

# Python program to remove duplicate words# using Regular Expression or ReGex.import re# Function to validate the sentence# and remove the duplicate wordsdef removeDuplicateWords(input):# Regex to matching repeated wordsregex = r\(\w+)(?:\W+\1\b)+ eturn re.sub(regex, r\1, input, flags=re.IGNORECASE)# Driver Code# Test Case: 1str1 = "I am a big big girl in the big big world"print(removeDuplicateWords(str1))# Test Case: 2str2 = "old macdonald had a farm e i e i o"print(removeDuplicateWords(str2))# Test Case: 3str3 = "see ya ya"print(removeDuplicateWords(str3))# This code is contributed by yuvraj_chandra

I am a big girl in the

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。