【说话处理惩罚与Python】4.3风格的题目/4.4函数:布局化编程的根蒂根基/4.5更多关于函数

    添加时间:2013-5-25 点击量:

    4.3风格的题目


    具体请参考Python相干册本或者材料。


    4.4函数:布局化编程的根蒂根基


    #如何斗劲正规的写一个函数



    import

    re def get_text(file): “””Read text 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 = [Takecareofthesenseandthe
    ...
    soundswilltakecareofthemselves.
    ]
    >>>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)
    [
    eefeedesleefs.]

    重视,在这段代码中,last_letter作为参数传入了extract_property函数中。


    Python供给了更多的体式格式来定义函数作为其他函数的参数,即:lambda表达式


    这里有两个例子:


    1、


    >>>extract_property(lambda w:w[-1])
    [
    eefeedesleefs.]

    2、


    >>>sorted(sent)
    [
    .Takeandcarecareofofsensesounds

    takethethethemselveswill
    ]
    >>>
    sorted(sent, cmp)
    [
    .Takeandcarecareofofsensesounds

    takethethethemselveswill
    ]
    >>>sorted(sent, lambda
    x,y: cmp(len(y), len(x)))
    [
    themselvessoundssenseTakecarewilltakecare

    theandtheofof.]


    累计函数


    让我们先来对比两段代码:


    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 [aoftheandwill.] >>>sent = [Takecareofthesenseandthe, ... soundswilltakecareofthemselves.] >>>filter(is_content_word, sent) [Takecaresensesoundstakecarethemselves] >>>[w for win sent if is_content_word(w)] [Takecaresensesoundstakecarethemselves]

    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}







    彼此相爱,却不要让爱成了束缚:不如让它成为涌动的大海,两岸乃是你们的灵魂。互斟满杯,却不要同饮一杯。相赠面包,却不要共食一个。一起歌舞欢喜,却依然各自独立,相互交心,却不是让对方收藏。因为唯有生命之手,方能收容你们的心。站在一起却不要过于靠近。—— 纪伯伦《先知》
    分享到: