一道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. 展开
provide and which prints out the number of occurrences of each character in the file. 展开
1个回答
展开全部
#!/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])
报错了,可以发一下内容。我看是哪里不兼容。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询