1
下面的代碼:類型錯誤:列表索引必須是整數或片,而不是STR字典蟒蛇
with open("input.txt", "r") as f:
text = f.read()
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
res = {}
kol = 0
for buk in alphabet:
if buk in text:
kol += 1
if kol > 0:
for bukwa in text:
if bukwa in alphabet:
if bukwa not in res:
res[bukwa.upper()] = text.count(bukwa)
elif bukwa not in alphabet:
if bukwa not in res:
res[bukwa.upper()] = 0
res = sorted(res)
with open("output.txt", "w") as f:
for key in res:
f.write(key + " " + str(res[key]))
if kol == 0:
with open("output.txt", "w") as f:
f.write(-1)
而這裏的錯誤:
Traceback (most recent call last):
File "/home/tukanoid/Desktop/ejudge/analiz/analiz.py", line 23, in <module>
f.write(key + " " + str(res[key]))
TypeError: list indices must be integers or slices, not str
TNX很多關於幫助 – Tukanoid