【语言处理与Python】4.3风格的问题/4.4函数:结构化编程的基础/4.5更多关于函数
4.3风格的问题
详细请参考Python相关书籍或者资料。
4.4函数:结构化编程的基础
#怎样比较正规的写一个函数
import
re def get_text(file): “””Read text from a file,normailizing whites space and stripping HTML markup.””” text=….. …. return text
文档说明函数
docstring
def
accuracy(reference, test): """
Calculatethe fraction of test items that equal the correspondingreference items.
Givena list ofreference values and a corresponding list oftest values,
return the fraction of corresponding values that are equal.
In particular,return the fraction of indexes
{0<i<=len(test)}such that C{test[i]==reference[i]}.
>>>accuracy(['ADJ', 'N', 'V', 'N'], ['N', 'N', 'V', 'ADJ'])
0.5
@paramreference: Anordered list of reference values.
@typereference: C{list}
@paramtest: Alist of values to compareagainst the corresponding
reference values.
@typetest: C{list}
@rtype:C{float}
@raiseValueError:If C{reference}and C{length}donot have the
same length.
""" if len(reference) != len(test): raise ValueError("Listsmusthave the same length.") num_correct = 0 for x, yin izip(reference, test): if x==y: num_correct +=1 return float(num_correct) / len(reference
4.5更多关于函数
作为参数的函数
>>>sent = ['Take', 'care', 'of', 'the', 'sense', ',', 'and', 'the',
... 'sounds', 'will', 'take', 'care', 'of', 'themselves', '.']
>>>def extract_property(prop):
... return [prop(word) for wordin sent]
...
>>>extract_property(len)
[4, 4, 2,3, 5,1, 3,3, 6,4, 4,4, 2,10, 1]
>>>def last_letter(word):
... return word[-1]
>>>extract_property(last_letter)
['e', 'e', 'f', 'e', 'e', ',', 'd', 'e', 's', 'l', 'e', 'e', 'f', 's', '.']
注意,在这段代码中,last_letter作为参数传入了extract_property函数中。
Python提供了更多的方式来定义函数作为其他函数的参数,即:lambda表达式
这里有两个例子:
1、
>>>extract_property(lambda w:w[-1])
['e', 'e', 'f', 'e', 'e', ',', 'd', 'e', 's', 'l', 'e', 'e', 'f', 's', '.']
2、
>>>sorted(sent)
[',', '.', 'Take', 'and', 'care', 'care', 'of', 'of', 'sense', 'sounds',
'take', 'the', 'the', 'themselves', 'will']
>>>sorted(sent, cmp)
[',', '.', 'Take', 'and', 'care', 'care', 'of', 'of', 'sense', 'sounds',
'take', 'the', 'the', 'themselves', 'will']
>>>sorted(sent, lambda x,y: cmp(len(y), len(x)))
['themselves', 'sounds', 'sense', 'Take', 'care', 'will', 'take', 'care',
'the', 'and', 'the', 'of', 'of', ',', '.']
累计函数
让我们先来对比两段代码:
1、
def
search1(substring, words): result = [] for wordin words: if substring in word: result.append(word) return result
2、
def
search2(substring, words): for wordin words: if substring in word: yield word
第2种方式是更好的,这种方法通常更有效。因为函数只产生调用程序需要的数据,并不需要分配额外的内存来存储输出。
高阶函数
offilter():
>>>def is_content_word(word): ... return word.lower()not in ['a', 'of', 'the', 'and', 'will', ',', '.'] >>>sent = ['Take', 'care', 'of', 'the', 'sense', ',', 'and', 'the', ... 'sounds', 'will', 'take', 'care', 'of', 'themselves', '.'] >>>filter(is_content_word, sent) ['Take', 'care', 'sense', 'sounds', 'take', 'care', 'themselves'] >>>[w for win sent if is_content_word(w)] ['Take', 'care', 'sense', 'sounds', 'take', 'care', 'themselves']
map():
在讨论这个函数之前,先来看两段程序:
1、
>>>lengths = map(len,nltk.corpus.brown.sents(categories='news')) >>>sum(lengths) / len(lengths) 21.7508111616
2、
>>>lengths = [len(w) for win nltk.corpus.brown.sents(categories='news'))] >>>sum(lengths) / len(lengths) 21.7508111616
两段代码的作用是一样的。
让我们再来看两段代码,体会一下:
1、
>>>map(lambdaw:len(filter(lambda c: c.lower() in "aeiou", w)),sent) [2, 2, 1,1, 2,0, 1,1, 2,1, 2,2, 1,3, 0]
2、
>>>[len([c for c in wif c.lower()in "aeiou"]) for win sent]
[2, 2, 1,1, 2,0, 1,1, 2,1, 2,2, 1,3, 0]
参数的命名
关键字参数:我们给变量有了明确的名字
任意数量未命名参数:
def generic(*args,**kwargs):print args
print kwargs
#得到的结果是:
generic(1,"African swallow", monty="python")
(1, 'African swallow')
{'monty':'python'}
以上是 【语言处理与Python】4.3风格的问题/4.4函数:结构化编程的基础/4.5更多关于函数 的全部内容, 来源链接: utcz.com/z/386644.html