【语言处理与Python】4.3风格的问题/4.4函数:结构化编程的基础/4.5更多关于函数
2013-05-24 23:24
961 查看
4.3风格的问题
详细请参考Python相关书籍或者资料。
4.4函数:结构化编程的基础
#怎样比较正规的写一个函数
re def get_text(file): “””Read text from a file,normailizing whites space and stripping HTML markup.””” text=….. …. return text
文档说明函数
docstring
accuracy(reference, test): """
""" 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、
search1(substring, words): result = [] for wordin words: if substring in word: result.append(word) return result
2、
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]
参数的命名
关键字参数:我们给变量有了明确的名字
任意数量未命名参数:
详细请参考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基础知识函数缓存,你需要知道的事
- 【脚本语言系列】关于Python基础知识异常处理,你需要知道的事
- 【语言处理与Python】2.3更多关于Python:代码重用
- 【脚本语言系列】关于Python基础知识处理字符串,你需要知道的事
- 数据处理的两个基本问题01 - 零基础入门学习汇编语言38
- 【脚本语言系列】关于Python基础知识面向对象编程,你需要知道的事
- 数据处理的两个基本问题04 - 零基础入门学习汇编语言41
- 关于Python中的 print 函数的缩进问题
- Go语言基础入门--函数,错误处理
- C语言基础--常用字符串处理函数
- 【脚本语言系列】关于Python数据库处理SQLite数据库,你需要知道的事
- Python Set集合,函数,深入拷贝,浅入拷贝,文件处理--小白基础
- Python基础笔记(1):函数篇(1):关于函数参数
- 【脚本语言系列】关于Python基础知识设计模式,你需要知道的事
- python语言基础问题汇总
- 数据处理的两个基本问题01 - 零基础入门学习汇编语言38
- 【脚本语言系列】关于Python结构化文本文件处理xml,你需要知道的事
- 【脚本语言系列】关于Python基础知识对象变动,你知道的事
- python中使用xmltodic处理xml文件,关于其中的列表问题。
- C语言基础-04-字符串和处理函数