python-3.x - I am trying to run following lines of codes
问题描述
Can someone help me with what is wrong with this code?
fname=input("Enter the name of the file:")
fhandle=open(fname)
word=list()
for line in fhandle: #All lines starting with "From " are split into list of words
if not line.startswith("From "):
continue
words=line.split()
word=words[1] #Second word is obtained
#Dictionary to count occurence
counts=dict()
for name in word:
counts[name] = counts.get(name,0)+1
print(counts[name],name)
print(counts)
#Deciding the maximum occurence of name
maxcount=None
maxword=None
for word,count in counts.items():
if maxcount is None or count > maxcount: #if we are ont first word or if
the current count is greater than previous maximum count
maxcount=count
maxword=word
print(maxword,maxcount)
The output I expected was to print OUTPUT: cwen@iupui.edu 5 but i end up getting None None as output. Can anybody help me cause I'm stuck from past 4 hrs!
解决方案
推荐阅读
- python - 无法为 Python3.6.5 安装 LIbraries
- python - 获取等于 100 的百分比列表
- typescript - 通过 Id (Observable) 值将 HTTP GET 传递给组件上的变量
- javascript - 在路由内部模拟一个 api 函数调用而不实际调用它
- vue.js - vue延迟路由不显示新的块js文件
- angular - NGXS:ofActionSuccessful 没有被触发,但是动作在 Redux Devtools 上显示
- java - 计算“a”和“z”之间的字母字符
- vue.js - 如何将 asyncData 外包给 Vuex Store?
- c - 显示记录功能显示非空记录不显示记录
- python - 如何迭代整数对列表,计算新的“联合对”