inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

프로그래밍 시작하기 : 도전! 45가지 파이썬 기초 문법 실습 (Inflearn Original)

문자열 파싱2(String Split By Delimiter(Advanced))

정답 오류

270

건민

작성한 질문수 9

0

 

in_str = "../source/22-1.txt"

with open(in_str, 'r') as file:

txt = file.read()

txt = txt.replace(","," ")

txt_list = txt.split()

print(txt_list)

print(len(txt_list))

 

 

 

영상속에서는 split()을 기본값으로 설정을 안하고

split(" ") 으로 해서 which\npermits 을 한단어로 인식해 정답이 72로 나오는데 기본값으로 하면 73으로 나옵니다

저거는 왜 인식이 안되서 72가 정답이되는거죠?

정규표현식으로 했을때도

저 단어만 인식을 못하고있습니다

 

python

답변 2

0

김도윤

저는 \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)

0

좋은사람

안녕하세요. 건민님 의견 감사합니다.

해당 소스코드 확인 후 오류일 경우 수정해서 반영하겠습니다.

감사드립니다.

dict, zip

0

21

2

for, range 추가 방법

0

39

0

오타: 20-1 예시 코드에 c = 135가 아니고 a = 135이죠?

0

77

1

방법2, 방법3의 결과가 요구 출력결과와 다릅니다.

0

125

4

glob.glob문 오류

0

71

1

질문입니다.

0

65

1

기초적인 질문입니다.

0

56

1

딕셔너리 인트문제

0

53

0

아나콘다 설치 시 오류가 뜨면서 인스톨이 끝까지 안 되네요...

0

123

1

실행오류

0

65

2

주피터 노트북 새버전

0

134

2

with open

0

131

2

PowerShell Prompt에서 주피터노트북 기본 드라이버/폴더 위치변경 후 주피터노트북 실행

0

186

2

기초적인 질문입니다만

0

113

1

새로운 dict를 생성한 이유에 대해서 궁금합니다.

0

169

2

안녕하세요 전 버전이 상위버전인지 달라서

0

245

2

주피터 token

0

142

2

주피터 token

0

166

2

아나콘다를 D드라이브에 설치했는데

0

721

4

python 3 (ipykernel)이 뜨지 않음

0

203

1

안돼요ㅜㅜ in[]으로 뜨지도 않고 초록색으로 변하지도 않아요...

1

346

3

주피터를 크롬말고 엣지에서 그대로 써도 괜찮은가요?

0

259

2

오류

0

149

1

주피터 실행 불가 에러

0

413

2