RE를 사용하지 않고 Python 문자열 목록에서 음절 계산
Nov 15 2020
텍스트 파일의 음절 수를 세어야합니다. 내 문제는 각 문자열의 각 문자를 반복하는 방법을 모른다는 것입니다. 내 생각은 한 글자가 모음인지 확인하고, 다음 글자가 모음이 아닌 경우 1 씩 증가시키는 것이 었습니다.하지만 "문자"를 늘릴 수는 없습니다. 나는 또한 "범위"방법을 사용하려고 시도했지만 그것도 문제가 있습니다. 무엇을 시도 할 수 있습니까? 감사합니다. 추신 : 파이썬 내장 메서드 만 사용할 수 있습니다.
txt = [ 'countingwords', 'house', 'plant', 'alpha', 'syllables']
이것은 지금까지 내 코드입니다.
def syllables(text_file):
count = 0
vowels = ['a','e','i','o','u','y']
with open(text_file, 'r') as f:
txt = f.readlines()
txt = [line.replace(' ','') for line in txt]
txt = [line.replace(',','') for line in txt]
txt = [y.lower() for y in txt]
for word in txt:
for letter in word:
if letter is in vowel and [letter + 1] is not in vowel:
count += 1
답변
cknoll Nov 14 2020 at 23:32
이것을 시도해 볼 수 있습니다.
lines = ["You should count me too"]
count = 0
vowels = "aeiouy"
for line in lines:
for word in line.lower().split(" "):
for i in range(len(word)):
if word[i] in vowels and (i == 0 or word[i-1] not in vowels):
count +=1
print(count) # -> 5