本文始发于个人公众号:TechFlow,原创不易,求个关注
<br>
<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="font-size: 16px; color: black; padding: 10px; line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; word-break: break-word; word-wrap: break-word; text-align: left; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;"><p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">今天是<strong style="font-weight: bold; color: rgb(71, 193, 168);">Python专题第6篇</strong>文章,给大家介绍的是Python当中三个非常神奇的方法:map、reduce和filter。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">不知道大家看到map和reduce的时候有没有什么感觉,如果看过之前我们大数据系列介绍MapReduce文章的同学,想必有些印象。这个MapReduce不是一个分布式的计算方法么,怎么又变成Python中的方法了?其实原因很简单,因为Python是一门很年轻的语言,它在发展的过程当中<strong style="font-weight: bold; color: rgb(71, 193, 168);">吸收了很多其他领域的精华</strong>,MapReduce就是其中之一。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">对之前文章感兴趣的同学可以点击下方的链接,回顾一下之前MapReduce的内容。</p> </section>
<section> <h2 data-tool="mdnice编辑器" style="font-weight: bold; font-size: 24px; border-bottom: 2px solid rgb(89,89,89); margin-bottom: 50px; margin-top: 100px; color: rgb(89,89,89);"><span style="font-size: 22px; display: inline-block; border-bottom: 2px solid rgb(89,89,89);">map</span></h2> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">map除了地图之外,另一个英文本意是映射。在C++和Java一些语言当中,将map进一步引申成了存储key和value映射结构的容器。Python对这点做了区分,KV结构的容器命名成了dict,即字典,而map则回到了它的本意,也就是<strong style="font-weight: bold; color: rgb(71, 193, 168);">映射</strong>。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">我们都知道,在数学领域,映射也是函数的定义。一个自变量通过某种映射,对应到一个因变量。同样,在Python当中,map操作本质也是函数,不过它作用的范围不再是单个变量,而是一个序列。换句话说,通过map我们可以省去循环操作,可以自动将一个容器当中的元素套用一个函数。</p> <figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px;"><img src="https://user-gold-cdn.xitu.io/2020/3/17/170e5e0e296db291?w=764&h=421&f=png&s=43801" alt style="display: block; margin: 0 auto; width: auto; max-width: 100%;"></figure> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">举个简单的例子,比如我们有一个坐标,我们希望知道它距离原点的距离。这个问题很简单,我们写一个计算距离的函数就可以解决:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #383a42; background: #fafafa; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">def</span> <span class="hljs-title" style="color: #4078f2; line-height: 26px;">dis</span><span class="hljs-params" style="line-height: 26px;">(point)</span>:</span><br> <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">return</span> math.sqrt(point[<span class="hljs-number" style="color: #986801; line-height: 26px;">0</span>]**<span class="hljs-number" style="color: #986801; line-height: 26px;">2</span> + point[<span class="hljs-number" style="color: #986801; line-height: 26px;">1</span>]**<span class="hljs-number" style="color: #986801; line-height: 26px;">2</span>)<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">那如果我有多个点需要计算距离,在map出现之前,我们只能用循环来解决问题:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #383a42; background: #fafafa; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">points = [[<span class="hljs-number" style="color: #986801; line-height: 26px;">0</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">1</span>], [<span class="hljs-number" style="color: #986801; line-height: 26px;">2</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">4</span>], [<span class="hljs-number" style="color: #986801; line-height: 26px;">3</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">2</span>]]<br><br><span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">for</span> point <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">in</span> points:<br> print(dis(point))<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">但是有了map之后, 我们可以省去循环的操作,整个代码简化成了一行:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #383a42; background: #fafafa; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">map(dis, points)<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">但是要注意,我们调用完map之后得到的结果不是一个list而是一个迭代器。我们直接将map返回的内容print出来,可以得到这样一个结果:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #383a42; background: #fafafa; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-meta" style="color: #4078f2; line-height: 26px;">>>> </span>print(map(dis, points))<br><map object at <span class="hljs-number" style="color: #986801; line-height: 26px;">0x107aad1d0</span>><br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">这是一个类的标准输出,其实它返回的不是最后的结果,而是一个迭代器。我们在之前的文章当中已经介绍过了迭代器和生成器的相关概念,这里不多做赘述了,遗忘的同学可以点击下方链接回顾一下之前的内容:</p> </section>
<section> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">我们想要获得完整的内容也很容易,我们只需要将它<strong style="font-weight: bold; color: rgb(71, 193, 168);">转化成list类型</strong>即可:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #383a42; background: #fafafa; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-meta" style="color: #4078f2; line-height: 26px;">>>> </span>print(list(map(dis, points)))<br>[<span class="hljs-number" style="color: #986801; line-height: 26px;">1.0</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">4.47213595499958</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">3.605551275463989</span>]<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">以上过程还可以进一步简化,还记得我们之前介绍过的<strong style="font-weight: bold; color: rgb(71, 193, 168);">匿名函数</strong>吗?由于dis函数在我们的程序当中只会在map中用到,我们完全没有必要单独创建一个函数,我们可以直接传入一个匿名函数搞定运算:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #383a42; background: #fafafa; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">map(<span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">lambda</span> x: math.sqrt(x[<span class="hljs-number" style="color: #986801; line-height: 26px;">0</span>]**<span class="hljs-number" style="color: #986801; line-height: 26px;">2</span> + x[<span class="hljs-number" style="color: #986801; line-height: 26px;">1</span>] ** <span class="hljs-number" style="color: #986801; line-height: 26px;">2</span>), points)<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">简单总结一下,map操作其实<strong style="font-weight: bold; color: rgb(71, 193, 168);">执行的是一个映射</strong>。它可以自动地将一个序列当中的内容通过制定的函数映射成另一个序列,从而避免显式地使用循环来调用,在很多场景下可以大大地简化代码的编写,可以很方便地将一个序列整体转变成另一个结果。</p> <h2 data-tool="mdnice编辑器" style="font-weight: bold; font-size: 24px; border-bottom: 2px solid rgb(89,89,89); margin-bottom: 50px; margin-top: 100px; color: rgb(89,89,89);"><span style="font-size: 22px; display: inline-block; border-bottom: 2px solid rgb(89,89,89);">reduce</span></h2> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">相比于map,reduce的操作稍稍难理解一点点。它也是规定一个映射,不过不是将一个元素映射成一个结果。而是将<strong style="font-weight: bold; color: rgb(71, 193, 168);">两个元素归并成一个结果</strong>。并且它并不是调用一次,而是依次调用,直到最后只剩下一个结果为止。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">比如说我们有一个数组[a, b, c, d]和一个函数f,我们计算reduce(f, [a, b, c, d])其实就等价于f(f(f(a, b), c), d)。和map不同的是,reduce最后得到一个结果,而不是一个迭代器或者是list。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">我们光说有些抽象,不妨来看一个例子,就看最简单的一个例子:reduce函数接收两个数,返回两个数的和。那么显然,我们依次调用reduce,得到的就是原数组的和。</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #383a42; background: #fafafa; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">from</span> functools <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">import</span> reduce<br><br><span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">def</span> <span class="hljs-title" style="color: #4078f2; line-height: 26px;">f</span><span class="hljs-params" style="line-height: 26px;">(a, b)</span>:</span><br> <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">return</span> a + b<br> <br>print(reduce(f, [<span class="hljs-number" style="color: #986801; line-height: 26px;">1</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">2</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">3</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">4</span>]))<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">最终得到的结果当然是10,同样,我们也可以将reduce中的方法定义成匿名函数,一样不影响最终的结果。</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #383a42; background: #fafafa; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">print(reduce(<span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">lambda</span> x, y: x + y, [<span class="hljs-number" style="color: #986801; line-height: 26px;">1</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">2</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">3</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">4</span>]))<br></code></pre> <h2 data-tool="mdnice编辑器" style="font-weight: bold; font-size: 24px; border-bottom: 2px solid rgb(89,89,89); margin-bottom: 50px; margin-top: 100px; color: rgb(89,89,89);"><span style="font-size: 22px; display: inline-block; border-bottom: 2px solid rgb(89,89,89);">MapReduce</span></h2> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">既然我们map和reduce都有了,显然我们可以将它们<strong style="font-weight: bold; color: rgb(71, 193, 168);">串联起来使用</strong>,也就是分布式系统当中MapReduce的做法。虽然如果不手动使用线程池的话,Python并不会起多个线程来加速运算,但是至少可以简化我们实现的代码。我们还是举经典的wordCount的例子,也就是<strong style="font-weight: bold; color: rgb(71, 193, 168);">文本计算词频</strong>。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">套用map和reduce的功能,整个流程非常清晰,我们只需要在map阶段对文本进行分词,在reduce阶段对分词之后的结果进行汇总即可。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">听着好像非常容易,但是你实际去上手是写不出来的。原因也很简单,因为hadoop当中的Map和Reduce中间还有一层shuffle的操作,<strong style="font-weight: bold; color: rgb(71, 193, 168);">会自动地将key值相同的结果放到同一个reducer当中</strong>。在这个问题当中,key自然就是我们的word,由于相同的word被放到同一个reducer当中,我们只需要累加就行了。但是如果我们自己编写mapreduce的话,由于缺少了中间数据重排的步骤,所以导致不能实现。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">要解决也简单,我们可以人为增加一个map阶段代替hadoop当中的重排。相当于做了一个MapMapReduce,我们来看代码:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #383a42; background: #fafafa; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">from</span> collections <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">import</span> Counter, defaultdict<br><br>texts = [<span class="hljs-string" style="color: #50a14f; line-height: 26px;">'apple bear peach grape'</span>, <span class="hljs-string" style="color: #50a14f; line-height: 26px;">'grape orange pear'</span>]<br><br><span class="hljs-comment" style="color: #a0a1a7; font-style: italic; line-height: 26px;"># 第一次map,将字符串转成数组,每个单词对应1</span><br><span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">def</span> <span class="hljs-title" style="color: #4078f2; line-height: 26px;">mp1</span><span class="hljs-params" style="line-height: 26px;">(text)</span>:</span><br> ret = []<br> words = text.split(<span class="hljs-string" style="color: #50a14f; line-height: 26px;">' '</span>)<br> <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">for</span> word <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">in</span> words:<br> ret.append((word, <span class="hljs-number" style="color: #986801; line-height: 26px;">1</span>))<br> <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">return</span> ret<br><br><br><span class="hljs-comment" style="color: #a0a1a7; font-style: italic; line-height: 26px;"># 第二次map,将数组转成dict</span><br><span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">def</span> <span class="hljs-title" style="color: #4078f2; line-height: 26px;">mp2</span><span class="hljs-params" style="line-height: 26px;">(arr)</span>:</span><br> d = defaultdict(int)<br> <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">for</span> k, v <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">in</span> arr:<br> d[k] += v<br> <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">return</span> d<br><br><span class="hljs-comment" style="color: #a0a1a7; font-style: italic; line-height: 26px;"># reduce,合并dict</span><br><span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">def</span> <span class="hljs-title" style="color: #4078f2; line-height: 26px;">rd</span><span class="hljs-params" style="line-height: 26px;">(x, y)</span>:</span><br> x.update(y)<br> <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">return</span> x<br><br>print(reduce(rd, map(mp2, map(mp1, texts))))<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">那如果我们不用多次MapReduce呢?也不是没有办法,需要取点巧,方法也简单只要使用之前我们讲解过的Counter类,就可以完美解决这个问题。我们来看代码:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #383a42; background: #fafafa; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">from</span> collections <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">import</span> Counter<br><br>texts = [<span class="hljs-string" style="color: #50a14f; line-height: 26px;">'apple bear peach grape'</span>, <span class="hljs-string" style="color: #50a14f; line-height: 26px;">'grape orange pear'</span>]<br><br><span class="hljs-function" style="line-height: 26px;"><span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">def</span> <span class="hljs-title" style="color: #4078f2; line-height: 26px;">mp</span><span class="hljs-params" style="line-height: 26px;">(text)</span>:</span><br> words = text.split(<span class="hljs-string" style="color: #50a14f; line-height: 26px;">' '</span>)<br> <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">return</span> Counter(words)<br><br>print(reduce(<span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">lambda</span> x, y: x + y, map(mp, texts)))<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">由于我们使用了Counter,所以我们在map阶段返回的结果就已经是词频的dict了,而在reduce阶段我们只需要将它们全部累加起来就OK了。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">最后,我们来看下<strong style="font-weight: bold; color: rgb(71, 193, 168);">filter</strong>。</p> <h2 data-tool="mdnice编辑器" style="font-weight: bold; font-size: 24px; border-bottom: 2px solid rgb(89,89,89); margin-bottom: 50px; margin-top: 100px; color: rgb(89,89,89);"><span style="font-size: 22px; display: inline-block; border-bottom: 2px solid rgb(89,89,89);">filter</span></h2> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">filter的英文是过滤,所以它的使用就很明显了。它的用法和map有些类似,我们编写一个函数来判断元素是否合法。通过调用filter,会自动将这个函数应用到容器当中所有的元素上,最后只会<strong style="font-weight: bold; color: rgb(71, 193, 168);">保留运行结果是True的元素,而过滤掉那些是False的元素</strong>。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">举个例子,假设我们想要保留list当中的奇数而过滤掉偶数,我们当然可以直接操作,比如:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #383a42; background: #fafafa; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">arr = [<span class="hljs-number" style="color: #986801; line-height: 26px;">1</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">3</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">2</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">4</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">5</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">8</span>]<br><br>[i <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">for</span> i <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">in</span> arr <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">if</span> i % <span class="hljs-number" style="color: #986801; line-height: 26px;">2</span> > <span class="hljs-number" style="color: #986801; line-height: 26px;">0</span> ]<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">而使用filter会非常方便:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #383a42; background: #fafafa; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">list(filter(<span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">lambda</span> x: x % <span class="hljs-number" style="color: #986801; line-height: 26px;">2</span> > <span class="hljs-number" style="color: #986801; line-height: 26px;">0</span>, arr))<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">从这个例子当中可能看不出便捷,但是有的时候判断的条件可能非常复杂,我们判断的逻辑不能简单地在list定义当中表达出来,这个时候使用filter则会容易得多。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">最后, 我们再看一个类似的用法。在itertools当中有一个方法叫做 <strong style="font-weight: bold; color: rgb(71, 193, 168);">compress</strong>,通过compress我们可以实现根据一个序列的条件过滤另一个序列。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">举个简单的例子,假设,我们有两个数组:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #383a42; background: #fafafa; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">student = [<span class="hljs-string" style="color: #50a14f; line-height: 26px;">'xiaoming'</span>, <span class="hljs-string" style="color: #50a14f; line-height: 26px;">'xiaohong'</span>, <span class="hljs-string" style="color: #50a14f; line-height: 26px;">'xiaoli'</span>, <span class="hljs-string" style="color: #50a14f; line-height: 26px;">'emily'</span>]<br>scores = [<span class="hljs-number" style="color: #986801; line-height: 26px;">60</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">70</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">80</span>, <span class="hljs-number" style="color: #986801; line-height: 26px;">40</span>]<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">我们想要获取所有考试及格的同学的list,如果用常规做法基本上免不了使用循环,但是使用compress可以很方便地通过一行代码实现:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #383a42; background: #fafafa; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">from</span> itemtools <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">import</span> compress<br><br><span class="hljs-meta" style="color: #4078f2; line-height: 26px;">>>> </span><span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">pass</span> = [i > <span class="hljs-number" style="color: #986801; line-height: 26px;">60</span> <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">for</span> i <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">in</span> scores]<br><span class="hljs-meta" style="color: #4078f2; line-height: 26px;">>>> </span>print(<span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">pass</span>)<br>[<span class="hljs-literal" style="color: #0184bb; line-height: 26px;">False</span>, <span class="hljs-literal" style="color: #0184bb; line-height: 26px;">True</span>, <span class="hljs-literal" style="color: #0184bb; line-height: 26px;">True</span>, <span class="hljs-literal" style="color: #0184bb; line-height: 26px;">False</span>]<br><br><span class="hljs-meta" style="color: #4078f2; line-height: 26px;">>>> </span>list(compress(student, <span class="hljs-keyword" style="color: #a626a4; line-height: 26px;">pass</span>))<br>[<span class="hljs-string" style="color: #50a14f; line-height: 26px;">'xiaohong'</span>, <span class="hljs-string" style="color: #50a14f; line-height: 26px;">'xiaoli'</span>]<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">需要注意的是filter和compress返回的都是一个迭代器,我们要获取它们的值,需要<strong style="font-weight: bold; color: rgb(71, 193, 168);">手动转换成list</strong>。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">虽然在日常的开发当中不使用这三样神器同样可以工作,但是用上它们之后,会提升很多代码的可读性,节省很多无用的代码。尤其是在面试的时候,很有可能就会给面试官留下不一样的印象,也许结果也会不同。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgb(89,89,89);">今天的文章就是这些,如果觉得有所收获,请顺手点个<strong style="font-weight: bold; color: rgb(71, 193, 168);">关注或者转发</strong>吧,你们的举手之劳对我来说很重要。</p> </section>
原文出处:https://www.cnblogs.com/techflow/p/12508455.html