[perl-python] 20050117,过滤器,地图 [英] [perl-python] 20050117, filter, map
问题描述
? # - * - 编码:utf-8 - * -
? #Python
?
? #thefilter功能可以用来
? #减少一个不需要的列表
? #要素被删除。
? #example:
?
? def even(n):返回n%2 == 0
?打印过滤器(偶数,范围(11))
?
?
? #themap功能应用功能
? #列表中的所有元素。例如:
?
? def square(n):返回n * n
?打印地图(方形,范围(11))
?
? ------------------------------------
? #cartl perl版本
?
?使用Data :: Dumper;
? sub even {return $ _ [0]%2 == 0};
?打印翻斗车[grep {偶数$ _}(0..10)];
?
?
? #sub square {$ n = shift; $ n ** 2;};
? sub square {(shift)** 2;};
?打印翻斗车[map {square($ _)}(0..10)];
?
? #上面的代码展示了一些语法
? #变体常见于perl代码中
? #base
?
? -------------------------
?
? #例如在Mathematica中,过滤器是
? #akin to Apply和map是MapThread。
? #可以应用标准地图
? #根据树木的等级规格
? #还有MapAt,Nest,
? #NestList ...任何更强大的
? #操纵树木的功能。
? #lisp语言通常有一个子集
?它的价格。
?
? ----------------------------
?注意:这篇文章来自Perl-Python
?
的一日邮件列表? http://groups.yahoo.com/group/perl-python/
?订阅,发送电子邮件至
? pe **************** ***@yahoogroups.com 如果
?你正在网页上阅读,
?程序示例可能无法运行,因为
? HTML转换经常会破坏代码。
?
? Xah
? xa*@xahlee.org
? http://xahlee.org/PageTwo_dir/more.html
? # -*- coding: utf-8 -*-
? # Python
?
? # the "filter" function can be used to
? # reduce a list such that unwanted
? # elements are removed.
? # example:
?
? def even(n): return n % 2 == 0
? print filter( even, range(11))
?
?
? # the "map" function applies a function
? # to all elements of a list. Example:
?
? def square(n): return n*n
? print map(square, range(11))
?
? ------------------------------------
? # similar perl versions
?
? use Data::Dumper;
? sub even {return $_[0]%2==0};
? print Dumper[ grep {even $_} (0..10)];
?
?
? # sub square {$n=shift;$n**2;};
? sub square {(shift)**2;};
? print Dumper [ map {square($_)} (0..10)];
?
? # the codes above showcase some syntax
? # variations commonly found in perl code
? # base
?
? -------------------------
?
? # in Mathematica for example, filter is
? # akin to Apply and map is MapThread.
? # The standard Map can be applied
? # according to a level spec of trees
? # and there''s also MapAt, Nest,
? # NestList...any many more powerful
? # functions that manipulates trees.
? # lisp languages often have a subset
? # of it.
?
? ----------------------------
? Note: this post is from the Perl-Python
? a-day mailing list at
? http://groups.yahoo.com/group/perl-python/
? to subscribe, send an email to
? pe*******************@yahoogroups.com if
? you are reading it on a web page,
? program examples may not run because
? html conversion often breaks the code.
?
? Xah
? xa*@xahlee.org
? http://xahlee.org/PageTwo_dir/more.html
推荐答案
_ [0]%2 == 0};
? print Dumper [grep {even
_[0]%2==0};
? print Dumper[ grep {even
_}(0..10)];
?
?
? #sub square {
_} (0..10)];
?
?
? # sub square {
n = shift;
n=shift;
这篇关于[perl-python] 20050117,过滤器,地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!