一道python的题,求大神帮忙

Writeaprogramwhichusesadictionarythatreadsinafile,HW6problem6.txt,thatIprovideandwhic... Write a program which uses a dictionary that reads in a file, HW6 problem 6.txt , that I

provide and which prints out the number of occurrences of each character in the file.
展开
 我来答
礻斤丶
推荐于2016-02-22 · TA获得超过4306个赞
知道大有可为答主
回答量:1544
采纳率:100%
帮助的人:1726万
展开全部
#!/usr/bin/python
#coding: utf-8
occur = {}
file = open('HW6 problem 6.txt')
lines = [list(line) for line in file.readlines()]
for line in lines:
    for character in line:
        if character not in occur:
            occur[character] = 0
        occur[character]+=1
for i in range(26):
    key = chr(97+i)
    Key = chr(65+i)
    if key in occur:
        print key, occur[key]
    if Key in occur:
        print Key, occur[Key]

随便找了一个文章测试,测试结果:

更多追问追答
追问
我3.4的,你可以稍微改下么?
追答

3我没用过。但是听说好像主要是print语句去除了。你试试吧所有的print内容全部放在括号内。

#!/usr/bin/python
#coding: utf-8
occur = {}
file = open('HW6 problem 6.txt')
lines = [list(line) for line in file.readlines()]
for line in lines:
    for character in line:
        if character not in occur:
            occur[character] = 0
        occur[character]+=1
for i in range(26):
    key = chr(97+i)
    Key = chr(65+i)
    if key in occur:
        print(key, occur[key])
    if Key in occur:
        print(Key, occur[Key])

报错了,可以发一下内容。我看是哪里不兼容。。

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式