高人求救,程序,python

来源:百度知道 编辑:UC知道 时间:2024/05/28 11:43:08
The function topwords(F,cutoff) is described here:
1. parameter F is a string, which should be the name
of a text file in the same folder (directory) as
this program
2. paramater cutoff is an integer
3. the result of topwords(F) should be a sorted list
of words (converted to all lowercase) such that
each of these words occurs more than cutoff times
in the file.
这个定义怎么写?
高人能否再帮帮我?
The function quotewords(F) is described here:
1. parameter F is a string, which should be the name
of a text file in the same folder (directory) as
this program
2. the result of quotewords(F) should be a list of
the "words" in the file that contain a single
quote AND contain the letter B (capital letter B).
这个定义怎么写,我愿加分!

import re

def topwords(F,cutoff):
stream = open(F).read()
pat = re.compile('(\w+)')
L = pat.findall(stream)

D = {}
for word in L:
if word in D:
D[word]+=1
else:
D[word] = 1

result = []
for word, frequency in D.items():
if frequency > cutoff:
result.append(word)
return sorted(result)