pop
принимает индекс, который удален из списка, и возвращается значение, которое было в этом индексе.
Вы не задаете ему индекс - но максимальное значение - индексы в списках, конечно, должны быть целыми числами.
Вы могли бы что-то придумать с .find()
- или продумать весь ваш подход:
Иногда легче сделать что-то с некоторыми знаниями о библиотеках Python.
Особенно: коллекций. Счетчик
Это специализированный словарь, который хорош для подсчета, он пропустит весь ваш текст один раз и добавит / увеличит свои ключи для каждого символа. Вы вызываете .count()
26 раз - по одному разу для каждого символа в string.ascii_lowercase - каждый раз, пересекая всю строку, чтобы посчитать вхождение в нее одного символа.
from collections import Counter
import string
t = """This is not a hamlet text, just a few letters and words.
Including newlines to demonstrate how to optimize your approach.
Of counting character frequencies..."""
# if you want all characters starting with a zero count, you can add and remove them
# if you do not need all characters, skip this step and go to c.update()
c = Counter(string.ascii_lowercase) # all have count of 1
c.subtract(string.ascii_lowercase) # now all are present with count of 0
# count all characters, passes text once
c.update(t.lower()) # you can input your file.read() here, any iterable will do
# sum all character counts
totalSum = sum(c.values())
# get the countings ordered max to min as tuples (Char,Count), modify
# with list comprehension to your float values. They are still ordered
top_n = [ (a,b/totalSum) for a,b in c.most_common()]
# use this to strip f.e. .,! and \n from the output:
# top_n = [ (a,b/totalSum) for a,b in c.most_common() if a in string.ascii_lowercase]
import pprint
pprint.pprint(c)
pprint.pprint(top_n)
Выход:
Counter({' ': 22, 't': 15, 'e': 14, 'o': 11,
'n': 10, 'a': 9, 'i': 9, 'r': 8,
's': 8, 'c': 6, 'h': 5, 'u': 5,
'.': 5, 'd': 4, 'l': 4, 'w': 4,
'f': 3, 'm': 3, 'p': 3, 'g': 2,
'\n': 2, 'j': 1, 'q': 1, 'x': 1,
'y': 1, 'z': 1, ',': 1, 'b': 0,
'k': 0, 'v': 0})
[(' ', 0.13924050632911392), ('t', 0.0949367088607595),
('e', 0.08860759493670886), ('o', 0.06962025316455696),
('n', 0.06329113924050633), ('a', 0.056962025316455694),
('i', 0.056962025316455694), ('r', 0.05063291139240506),
('s', 0.05063291139240506), ('c', 0.0379746835443038),
('h', 0.03164556962025317), ('u', 0.03164556962025317),
('.', 0.03164556962025317), ('d', 0.02531645569620253),
('l', 0.02531645569620253), ('w', 0.02531645569620253),
('f', 0.0189873417721519), ('m', 0.0189873417721519),
('p', 0.0189873417721519), ('g', 0.012658227848101266),
('\n', 0.012658227848101266), ('j', 0.006329113924050633),
('q', 0.006329113924050633), ('x', 0.006329113924050633),
('y', 0.006329113924050633), ('z', 0.006329113924050633),
(',', 0.006329113924050633),
('b', 0.0), ('k', 0.0), ('v', 0.0)]