Python 3, подсчитать частоту букв в файле [дубликат]Python

Программы на Python
Anonymous
Python 3, подсчитать частоту букв в файле [дубликат]

Сообщение Anonymous »

Строка:
для Char в слове возвращает ошибку «TypeError: объект 'int' не повторяется»
Как он стал целым числом, а не строкой?
Как это повторить?

# Write a program that reads a file and prints the letters in decreasing
# order of frequency. Your program should convert all the input to lower
# case and only count the letters a-z. Your program should not count
# spaces, digits, punctuation, or anything other than the letters a-z.

import string
from string import digits

fname = input("Enter file:")
if len(fname) < 1 : fname = "romeo-full.txt"
fhand = open(fname)

charcount = dict()

for line in fhand:
line = line.rstrip()
line = line.lower()
line = line.translate(str.maketrans('','',string.punctuation))
line = str.maketrans("", "", digits)

for word in line:
for char in word:
charcount[char] = charcount.get(char, 0) + 1

print(charcount)


Подробнее здесь: https://stackoverflow.com/questions/591 ... cy-in-file

Вернуться в «Python»