高人求救,python

来源:百度知道 编辑:UC知道 时间:2024/06/24 17:10:24
这个定义怎么写?
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).
这个定义怎么写?
The function wordcount(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 wordcount(F) should be the number
of distinct words, without considering upper/lower
case, and exclusing any words that have punctuation.

Thus:
a) Do not count a word like finally!
because the ! is a punctuation character
b) The definition of what counts as
punctuation is the list of characters

和你前面问的问题很像啊
你就不能自己想一下啊

看来你自己是想不出来了,
看看下面的代码,哪里不明白留言问我

import re, string

def quotewords(F):
stream = open(F).read()
pat = re.compile('([^\s]+)')
L = pat.findall(stream)

result = []
for word in L:
if "'" in word and "B" in word:
result.append(word)
return result

def wordcount(F):
stream = open(F).read()
pat = re.compile('([^\s]+)')
L = pat.findall(stream)

result = []
for word in L:
has_punctuation = False
for letter in word:
if letter in string.punctuation:
has_punctuation = True
break
if not has_punctuation and not word.lower() in result:
result.append(word.lower())
return result