wordcount.py
来自「python web programming 部分」· Python 代码 · 共 18 行
PY
18 行
counter = {} # create empty directory
file = open("walrus.txt") # open file for input
while 1: # forever ...
line = file.readline() # read the next line
if line == "": # end of file?
break # yes: terminate loop
for w in line.split(): # for each word in the line
if counter.has_key(w): # have we seen this word before?
counter[w] += 1 # yes: add one to its count
else:
counter[w] = 1 # no: establish count of one
file.close() # tidy up
words = counter.keys() # extract a list of words found
words.sort() # sort the list
for w in words: # for each word in the list
print w, counter[w] # print the word and its count
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?