赖勇浩(http://laiyonghao.com)
今天早上,@smallfishxy 公开了一条面试题:
读取文件,统计文件中每个单词出现的次数,然后按照次数高低排序。
本来蛮平淡无奇的一题,但一跟前天介绍的 Pipe 结合起来,就有意思了,这类数据流的处理,相当适合用 Pipe 来处理,花了点时间,写代码如下:
from __future__ import print_function from re import split from pipe import * with open('test_descriptor.py') as f: print(f.read() | Pipe(lambda x:split('/W+', x)) | Pipe(lambda x:(i for i in x if i.strip())) | groupby(lambda x:x) | select(lambda x:(x[0], (x[1] | count))) | sort(key=lambda x:x[1], reverse=True) )
输出:
[('self', 13), ('foo', 9), ('item', 9), ('_data', 8), ('print', 7), ('def', 5), ('return', 5), ('Jeff', 4), ('i', 4), ('in', 4), ('jeff', 4), ('ken', 4), ('obj', 4), ('val', 4), ('class', 3), ('lai', 3), ('pan', 3), ('tmp', 3), ('Foo', 2), ('ItemDescriptor', 2), ('Wrapper', 2), ('__iter__', 2), ('for', 2), ('if', 2), ('next', 2), ('object', 2), ('0', 1), ('1', 1), ('30', 1), ('8', 1), ('None', 1), ('__class__', 1), ('__future__', 1), ('__get__', 1), ('__init__', 1), ('__set__', 1), ('bin', 1), ('coding', 1), ('env', 1), ('f', 1), ('from', 1), ('import', 1), ('instance', 1), ('isinstance', 1), ('len', 1), ('list', 1), ('print_function', 1), ('python', 1), ('type', 1), ('usr', 1), ('utf', 1)]
在使用 Pipe 解题的过程中,发现一个问题:
当出错的时候,想找到错误原因太难了!
您还没有登录,请您登录后再发表评论
前端工程师面试
The PHY Interface for the PCI Express, SATA, and USB SuperSpeed Architectures (PIPE) is intended to enable the development of functionally equivalent PCI Express, SATA and USB SuperSpeed PHY's....
使用Big pipe提升浏览速度 使用Big pipe提升浏览速度
USB3.0 PIPE总线协议 针对usb物理接口芯片,所使用的pipe 总线的详细解释
pipe flow expert 教程 2013版本
pipe2.4
This simple example illustrates using the Pipe Control.This example also demonstrates the following...1) Modifying basic control properties at runtime
Petri网建模软件——PIPE4.3.0
pipe4
PIPE THREAD
利用pipe进程间通信时,到底通信的是什么?此软件可以读取namedpipe的内容
多个进程利用pipe连接构成pipe ring, 利用这个环进行pipe sorting
细看PCIe接口PIPE规范,对高速接口学习很有帮助,很难得。
该文档为最新5.1的PIPE接口协议,讲述PCS与MAC层之间的数据通信 。
北大POJ1039-Pipe 解题报告+AC代码
进程间通信的一种方式,Pipes:管道,分为无名管道:在父子进程间交换数据;有名管道:可在不同主机间交换数据,分为服务器方和客户方,在Win9X下只支持有名管道客户。 1、进程间通信 2、管道Pipe通信
Android 一、选择题(30 题,每题 1.5 分,共 45 分) 1.java.io 包中定义了多个流类型来实现输入和输出功能,可以从不同的角度对其进行分 类,按功能分为:(),如果为读取的内容进行处理后再输出,需要使用下列哪种流...
mpipe.rar
vmware里装FreeBSD,装Junos,添加serial port,通过Pipe管道软件,建立串口Pipe与环回地址的管道,通过CRT,telnet(环回地址+端口号)登录设备的console控制台
相关推荐
前端工程师面试
The PHY Interface for the PCI Express, SATA, and USB SuperSpeed Architectures (PIPE) is intended to enable the development of functionally equivalent PCI Express, SATA and USB SuperSpeed PHY's....
使用Big pipe提升浏览速度 使用Big pipe提升浏览速度
USB3.0 PIPE总线协议 针对usb物理接口芯片,所使用的pipe 总线的详细解释
pipe flow expert 教程 2013版本
pipe2.4
This simple example illustrates using the Pipe Control.This example also demonstrates the following...1) Modifying basic control properties at runtime
Petri网建模软件——PIPE4.3.0
Petri网建模软件——PIPE4.3.0
pipe4
PIPE THREAD
利用pipe进程间通信时,到底通信的是什么?此软件可以读取namedpipe的内容
多个进程利用pipe连接构成pipe ring, 利用这个环进行pipe sorting
细看PCIe接口PIPE规范,对高速接口学习很有帮助,很难得。
该文档为最新5.1的PIPE接口协议,讲述PCS与MAC层之间的数据通信 。
北大POJ1039-Pipe 解题报告+AC代码
进程间通信的一种方式,Pipes:管道,分为无名管道:在父子进程间交换数据;有名管道:可在不同主机间交换数据,分为服务器方和客户方,在Win9X下只支持有名管道客户。 1、进程间通信 2、管道Pipe通信
Android 一、选择题(30 题,每题 1.5 分,共 45 分) 1.java.io 包中定义了多个流类型来实现输入和输出功能,可以从不同的角度对其进行分 类,按功能分为:(),如果为读取的内容进行处理后再输出,需要使用下列哪种流...
mpipe.rar
vmware里装FreeBSD,装Junos,添加serial port,通过Pipe管道软件,建立串口Pipe与环回地址的管道,通过CRT,telnet(环回地址+端口号)登录设备的console控制台