【发布时间】:2017-04-12 08:10:23
【问题描述】:
代码如下:
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
【问题讨论】:
标签: python string python-3.x dictionary