Return words with double consecutive letters

I'm trying to make a python program to return the words that have double consecutive letters, (e.g., door, ball, floor). My code until now is this the following, but it returns me all the words in the files divided in two-letter parts:

def text_processing( file_location ): import re file_variable = open( file_location ) lines = file_variable.read() print lines double_letter = re.compile('[A-Z]{2,2}', re.IGNORECASE) double_letter_list = double_letter.findall(lines) print double_letter_list

Answer1:

You could try this:

def text_processing( file_location ): import re file_variable = open( file_location ) lines = file_variable.readlines() double_letter = re.compile(r'.*(.)\1.*', re.IGNORECASE) double_letter_list = [] for line in lines: for word in line.split(" "): match = double_letter.match(word) if match: double_letter_list.append(match.group()) print double_letter_list

It tries to match the pattern against each word in the file, and if it is a match it appends it to the list of double words.

Answer2:

Try this regex: r"\w*(\w)\1\w*"

Answer3:

re.findall('(\w*(\w)\\2\w*)', file_variable.read())

Will return a list of tuples (word, repeated letter), you can then just take all of the first elements.

Example:

>>> re.findall('(\w*(\w)\\2\w*)', 'some words here: boo, shoo, wooooo, etc.') [('boo', 'o'), ('shoo', 'o'), ('wooooo', 'o')]

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:Return words with double consecutive letters