1
ここでは、コードです:はTypeError:リスト指数は、整数またはスライスではなく、strの辞書のpythonでなければなりません
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