$ import scala.io.Source
$ val lines = Source.fromFile("/home/badou/Documents/code/mr/mr_wc/The_Man_of_Property.txt").getLines().toList
lines: List[String] = List(Preface, “The.....
$ lines.flatMap(_.split(" ")).map(x=>(x,1))
List[(String, Int)] = List((Preface,1), (“The,1), (....
$ lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(_._1)
scala.collection.immutable.Map[String,List[(String, Int)]] = Map(unlikely. -> List((unlikely.,1)), .......
$ lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(_._1).map(x=>(x._1,x._2.length))
scala.collection.immutable.Map[String,Int] = Map(unlikely. -> 1, come? -> 1, unexpectedly; -> 1,.....
<==>$ lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(_._1).map(x=>(x._1,x._2.size))
<==> $ lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(_._1).map(x=>(x._1,x._2.map(_._2).sum))
这三者输出都一样
排序(方法一)
$ lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(_._1).map(x=>(x._1,x._2.map(_._2).sum)).toList.sortBy(_._2)
List[(String, Int)] = List((unlikely.,1), (come?,1), (unexpectedly;,1), (easel.,1), (hand’,1), (buns,1), ....
降序排序
$ lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(_._1).map(x=>(x._1,x._2.map(_._2).sum)).toList.sortBy(_._2).reverse
List[(String, Int)] = List((the,5144), (of,3407), (to,2782), (and,2573), (a,2543), (he,2139), (his,1912),
取topN
$ lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(_._1).map(x=>(x._1,x._2.map(_._2).sum)).toList.sortBy(_._2).reverse.slice(0,10)
List[(String, Int)] = List((the,5144), (of,3407), (to,2782), (and,2573), (a,2543), (he,2139), (his,1912), (was,1702), (in,1694), (had,1526))
排序(方法2) sortWith
$ lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(_._1).map(x=>(x._1,x._2.map(_._2).sum)).toArray.sortWith(_._2>_._2).slice(0,10)
Array[(String, Int)] = Array((the,5144), (of,3407), (to,2782), (and,2573), (a,2543), (he,2139), (his,1912), (was,1702), (in,1694), (had,1526))
排序(方法2) toList和toArrray效果一样
$ lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(_._1).map(x=>(x._1,x._2.map(_._2).sum)).toList.sortWith(_._2>_._2).slice(0,10)
List[(String, Int)] = List((the,5144), (of,3407), (to,2782), (and,2573), (a,2543), (he,2139), (his,1912), (was,1702), (in,1694), (had,1526))
排序(方法3) mapValues()
$ lines.flatMap(_.split(" ")).map(x=>(x,1)).groupBy(_._1).mapValues(_.size).toArray.sortWith(_._2>_._2).slice(0,10)
Array[(String, Int)] = Array((the,5144), (of,3407), (to,2782), (and,2573), (a,2543), (he,2139), (his,1912), (was,1702), (in,1694), (had,1526))
详解:
$ val b = Array(("a",1),("b",2))
输出:b: Array[(String, Int)] = Array((a,1), (b,2))
$ b.sortWith((t1,t2)=>(t1._2>t2._2))
输出:Array[(String, Int)] = Array((b,2), (a,1))
tuple按照第二个值排序,对于传入的两个tuple,t1和t2,比较第二个值的大小,第二个值大的tuple排在前面。
$简写 b.sortWith(_._2>_._2)
输出:Array[(String, Int)] = Array((b,2), (a,1))
scala学习笔记
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 本文引用地址: 原地址,原地址2 为什么选择scala 火热的网上公开课网站Coursera采用了Scala来作为...
- 1. 偏函数 偏函数(Partial Function),是一个数学概念它不是"函数"的一种, 它跟函数是平行的概...
- 教材:快学Scalamarkdown阅读:https://www.zybuluo.com/mdeditor cha...
- 粉晶和红石榴石---逆转人际关系的破冰组合 客人C小姐,在银行柜台工作,性格内向的她,工作时遇到很多刁难的客户,主...