我不确定我在哪里收到此错误:
Traceback (most recent call last):
File "C:/Users/Coscio/PycharmProjects/unigram/letterHist.py", line 78, in <module>
main()
File "C:/Users/Coscio/PycharmProjects/unigram/letterHist.py", line 74, in main
bars(words)
File "C:/Users/Coscio/PycharmProjects/unigram/letterHist.py", line 62, in bars
init(words, lst)
File "C:/Users/Coscio/PycharmProjects/unigram/letterHist.py", line 38, in init
freqLegend(words,val, lst)
File "C:/Users/Coscio/PycharmProjects/unigram/letterHist.py", line 54, in freqLegend
freqLegend(words, val/1.5, counter-1)
File "C:/Users/Coscio/PycharmProjects/unigram/letterHist.py", line 53, in freqLegend
back((max(lst)*1000)/10)
TypeError: 'int' object is not iterable
我认为它发生在我的freqLegend函数中,我在其中取最大值lst并将其除以10但是我现在没有迭代任何东西而且我无法找到我收到此错误的原因。
from letterFreq import *
from turtle import *
def init(words, lst):
val = max(lst)
print(val)
speed(0)
setup (width=600, height=600, startx=0, starty=0)
up()
lt(180)
forward(200)
lt(90)
forward(100)
lt(90)
down()
forward(400)
up()
forward(-400)
lt(90)
down()
forward(val*1000)
up()
back(val*1000+20)
rt(90)
forward(200)
write("A B C D E F G H I J K L M N O P Q "
"R S T U V W X Y Z", True, align="center")
goto(-200,-100)
lt(90)
forward((val*1000)/2)
lt(90)
forward(60)
write("Frequency", True, align="center")
rt(90)
goto(-200,153)
freqLegend(words,val, lst)
def freqLegend(words, val, lst, counter = 10):
if counter == 0:
goto(-200,-100)
return
elif counter > 0:
up()
lt(90)
down()
forward(30)
write(round(val, 3 ))
back(30)
rt(90)
back((max(lst)*1000)/10)
freqLegend(words, val/1.5, counter-1)
def bars(words):
lstFreq = []
for letters in letterFreq(words):
lstFreq.append(letterFreq(words)[letters])
lst = lstFreq
init(words, lst)
for i in lst:
down()
forward(i*1000)
rt(90)
forward(14)
left(90)
back(i*1000)
def main():
filename = "data/very_short.csv"
words = readWordFile(filename)
bars(words)
input("enter to close")
if __name__ == '__main__':
main()
答案 0 :(得分:2)
你错过了freqLegend
的参数,所以你得到了你不期望的行为。你只有三个参数:
freqLegend(words, val/1.5, counter-1)
我认为你的意思
freqLegend(words, val/1.5, lst, counter-1)
你得到一个不可迭代的错误的原因是因为max()
实际遍历了列表。由于参数counter-1
作为参数lst
传递给函数,因此在第二个调用lst
上是一个int,它无法迭代以找到最大值。