시작하기무료로 시작하기

더 많은 문자열 연산자와 Twitter

이 연습 문제에서는 tweets 데이터셋에서 선택한 세 개의 문자열에 서로 다른 문자열 연산자를 적용해 봅니다. 이미 tweets_list가 준비되어 있어요.

서로 다른 문자열 연산자를 적용해 다음의 세 가지 새 리스트를 만들어 보세요:

  • 문자(letters)만 남긴 리스트
  • 영문자와 숫자 등 영숫자(alphanumeric)만 남긴 리스트
  • 숫자(digits)만 남긴 리스트

필요한 함수들은 nltk에서 미리 가져와 두었습니다.

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

Python으로 배우는 Sentiment Analysis

강의 보기

연습 안내

  • tweets_list의 토큰으로 구성된 리스트를 만드세요.
  • 리스트 letters에서는 숫자와 그 밖의 모든 문자를 제거하고, 알파벳 문자만 남기세요.
  • let_digits에서는 영숫자(alphanumeric)만 유지하고, 그 외의 모든 문자는 제거하세요.
  • digits는 문자와 특수문자를 제거하고 숫자만 남기도록 만드세요.

실습형 인터랙티브 연습

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

# Create a list of lists, containing the tokens from list_tweets
tokens = [____(item) for item in tweets_list]

# Remove characters and digits , i.e. retain only letters
letters = [[word for word in item if ____.____] for item in tokens]
# Remove characters, i.e. retain only letters and digits
let_digits = [[word for word in item if ____.____] for item in tokens]
# Remove letters and characters, retain only digits
digits = [[word for word in item if ____.____] for item in tokens]

# Print the last item in each list
print('Last item in alphabetic list: ', letters[2])
print('Last item in list of alphanumerics: ', let_digits[2])
print('Last item in the list of digits: ', digits[2])
코드 편집 및 실행