시작하기무료로 시작하기

Finding a substring

It's a new day at work and you need to continue cleaning your dataset for the movie prediction project. While exploring the dataset, you notice a strange pattern: there are some repeated, consecutive words occurring between the character at position 37 and the character at position 41. You decide to write a function to find out which movie reviews show this peculiarity, remembering that the ending position you specify is not inclusive. If you detect the word, you also want to change the string by replacing it with only one instance of the word.

Complete the if-else statement following the instructions.

The text of three movie reviews has been already saved in the variable movies. You can use print(movies) to view the variable in the IPython Shell.

이 연습은 강의의 일부입니다

Regular Expressions in Python

강의 보기

연습 안내

  • Find if the substring actor occurs between the characters with index 37 and 41 inclusive. If it is not detected, print the statement Word not found.
  • Replace actor actor with the substring actor if actor occurs only two repeated times.
  • Replace actor actor actor with the substring actor if actor appears three repeated times.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

for movie in movies:
  	# If actor is not found between character 37 and 41 inclusive
    # Print word not found
    if movie.find("____", ____, ____) == ____:
        print("Word not found")
    # Count occurrences and replace two with one
    elif movie.____("____") == 2:  
        print(movie.replace("____", "____"))
    else:
        # Replace three occurrences with one
        print(movie.replace("____", "____"))
코드 편집 및 실행