【发布时间】:2018-10-09 21:37:21
【问题描述】:
这是我第一次在 Stack Overflow 上提问,所以如果我的问题过于模糊或没有提供足够的信息,我提前道歉。
基本上我遇到的问题是我的代码由于 TypeError 而无法运行。
import string
f = open('data/hamlet.txt', 'r')
text = f.read()
alphabet_freq = []
for c in string.ascii_lowercase :
alphabet_freq.append(text.count(c) + text.count(c.upper()))
alphabet_freq_sum = 0
for _ in alphabet_freq :
alphabet_freq_sum +=_
letter_frequency = []
for _ in alphabet_freq :
letter_frequency.append(( _ / alphabet_freq_sum) * 100)
alphabets = list(string.ascii_lowercase)
letter_frequency_in_freq_order = []
for _ in letter_frequency :
letter_frequency_in_freq_order.append(letter_frequency.pop(max(letter_frequency)))
print(letter_frequency_in_freq_order,letter_frequency)
堆栈跟踪
**make: *** [py3_run] 오류 1
Traceback (most recent call last):
File "Main.out", line 26, in <module>
letter_frequency_in_freq_order.append(letter_frequency.pop(max(letter_frequency)))
TypeError: integer argument expected, got float**
我认为 fucntion max 在浮动时不起作用。正确的?
【问题讨论】:
-
您希望
letter_frequency.pop(a_float)做什么?你可能想要.remove。 -
下划线 (
_) 传统上用于您想要丢弃的值。像这样使用它会使您的代码更难阅读。 -
问题不在于
max函数。它具有pop功能。我推荐你看pop function doc。
标签: python python-3.x max