정답 오류
저는 \n, 컴마, 빈칸 으로 나눴고 \n 으로 나눈 경우 대부분 한 칸이 띄어져 있어서 단어의 리스트에 ""이 요소로 잡힙니다. 그래서 그냥 단어 리스트에서 ""를 제거하고 나니 73이 나오네요.import re # 방법 1: regular expression 사용 def how_many_words_in_a_text_file(path: str) -> (str, int): with open(path, "r", encoding="utf-8") as f: text = f.read() words = re.split(" |,|\n", text) words = [word for word in words if word != ""] return (words, len(words)) print(how_many_words_in_a_text_file("../source/22-1.txt")) --- 결과값 (['The', 'adjective', '"deep"', 'in', 'deep', 'learning', 'refers', 'to', 'the', 'use', 'of', 'multiple', 'layers', 'in', 'the', 'network.', 'Early', 'work', 'showed', 'that', 'a', 'linear', 'perceptron', 'cannot', 'be', 'a', 'universal', 'classifier', 'but', 'that', 'a', 'network', 'with', 'a', 'nonpolynomial', 'activation', 'function', 'with', 'one', 'hidden', 'layer', 'of', 'unbounded', 'width', 'can.', 'Deep', 'learning', 'is', 'a', 'modern', 'variation', 'which', 'is', 'concerned', 'with', 'an', 'unbounded', 'number', 'of', 'layers', 'of', 'bounded', 'size', 'which', 'permits', 'practical', 'application', 'and', 'optimized', 'implementation', 'while', 'retaining', 'theoretical'], 73) # 방법2 : 정규표현식 없이 replace와 split으로 def number_of_words_in_the_text(path: str) -> (str, int): with open(path, 'r', encoding="utf-8") as f: text = f.read() text = text.replace(",", " ").replace("\n", " ") words = text.split(" ") words = [word for word in words if word != ""] return (words, len(words)) print(number_of_words_in_the_text("../source/22-1.txt")) --- 결과값 (['The', 'adjective', '"deep"', 'in', 'deep', 'learning', 'refers', 'to', 'the', 'use', 'of', 'multiple', 'layers', 'in', 'the', 'network.', 'Early', 'work', 'showed', 'that', 'a', 'linear', 'perceptron', 'cannot', 'be', 'a', 'universal', 'classifier', 'but', 'that', 'a', 'network', 'with', 'a', 'nonpolynomial', 'activation', 'function', 'with', 'one', 'hidden', 'layer', 'of', 'unbounded', 'width', 'can.', 'Deep', 'learning', 'is', 'a', 'modern', 'variation', 'which', 'is', 'concerned', 'with', 'an', 'unbounded', 'number', 'of', 'layers', 'of', 'bounded', 'size', 'which', 'permits', 'practical', 'application', 'and', 'optimized', 'implementation', 'while', 'retaining', 'theoretical'], 73)