Средняя длина слова — пунктуация («,», «.», «;») и символы новой строки {«n»}

#python

#питон

Вопрос:

Я хочу написать скрипт на Python,который будет считывать тест с консоли и выводить среднее количество символов в слове, но у меня возникли некоторые проблемы с пунктуацией и символами новой строки . вот мой код.

 def main():  allwords = []  while True:  words = input()  if words == "Amen.":  break  allwords.extend(words.split())  txt = " ".join( allwords)  n_pismen = len([c for c in txt if c.isalpha()])  n_slov = len([i for i in range(len(txt) - 1) if txt[i].isalpha() and not txt[i   1].isalpha()])  for char in (',', '.', ';'):  txt = txt.replace(char, '')  txt.replace('n', ' ')  words = txt.split()  print(sum(len(word) for word in words) / len(words))  if words:  average = sum(len(words) for words in allwords) / len( allwords)   if __name__ == '__main__':  main()    
 Our Father, which art in heaven, hallowed be thy name; thy kingdom come; thy will be done, in earth as it is in heaven. Give us this day our daily bread. And forgive us our trespasses, as we forgive them that trespass against us. And lead us not into temptation, but deliver us from evil. For thine is the kingdom, the power, and the glory, For ever and ever. Amen.  

нормальным будет вывод 4,00,но я просто получаю 1,00

Ответ №1:

Не знаю, что не так в вашем примере, но это сработает. Я использовал «тест» в качестве имени строки, вы можете изменить его по своему желанию:

 counts = [] #List to store number of characters per word for t in test.split(): #Split to substrings at whitespace  counts.append(len([c for c in t if c.isalpha()])) #Calculate the length of each word ignoring non-letters print(sum(counts)/len(counts)) #Compute the average  

Ответ №2:

Вы можете сделать это следующим образом (где strng находится отрывок текста):

 # Remove all of the 'bad' characters for char in (',', '.', ';'):  strng = strng.replace(char, '') strng.replace('n', ' ') # Split on spaces words = strng.split() # Calculate the average length print(sum(len(word) for word in words) / len(words))  

Комментарии:

1. неэффективно выполнять несколько replace операций подряд, использовать re.sub или понимать список

2. Я просто скопировал эту часть из исходного вопроса, чтобы не добавлять изменений, которые могли бы отвлекать. re.sub или str.translate , скорее всего, будет работать лучше, в зависимости от введенного текста.

Ответ №3:

Я бы сопоставил каждое слово с помощью регулярного выражения, чем отслеживать # слов и # всего символов:

 import re  total_number = 0 n_words = 0  pattern = re.compile("[a-z] ", re.IGNORECASE)  with open({PATH_TO_YOUR_FILE}, "r") as f:  for line in f:  words = pattern.findall(line)  n_words  = len(words)  total_number  = sum([len(x) for x in words])  print(total_number/n_words)  

выход

 4.0   

Ответ №4:

Попробуйте это:

 import string  s = input() s = s.translate(str.maketrans('', '', string.punctuation) s.replace('n', ' ') words = s.split() print(sum(map(len, words)) / len(words))