我的代码所做的是计算一个字母出现的次数并将其计入受尊敬的字母。因此,如果 A 出现两次,它将显示 2:A。我的问题是我希望它从文件中读取,而当 ord() 尝试读取时,它不能。我不知道如何解决这个问题。
t=open('lettersTEst.txt','r')
tList=[0]*26
aL=['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']
idx=0
for char in t:
ch=ord(char)
if ch >=65 and ch <= 90:
pos=int(ch)-65
tList[pos]+=1
for ele in tList:
print(idx, ": ", tList[ch])
idx+=1