我试图通过保留元素的顺序来获取列表中单词的连续计数
scala> val a = List("she","sells","seashells","by","the","seashore","the", "shells", "she", "sells", "are", "surely", "seashells","where", "are", "the", "shells")
a: List[String] = List(she, sells, seashells, by, the, seashore, the, shells, she, sells, are, surely, seashells, where, are, the, shells)
scala> a.map( x => (x,a.count(_ == x)))
res13: List[(String, Int)] = List((she,2), (sells,2), (seashells,2), (by,1), (the,3), (seashore,1), (the,3), (shells,2), (she,2), (sells,2), (are,2), (surely,1), (seashells,2), (where,1), (are,2), (the,3), (shells,2))
scala>
Run Code Online (Sandbox Code Playgroud)
但是我想要的是
List((she,1), (sells,1), (seashells,1), (by,1), (the,1), (seashore,1), (the,2), (shells,1), (she,2), (sells,2), (are,1), (surely,1), (seashells,2), (where,1), (are,2), (the,3), (shells,2))
Run Code Online (Sandbox Code Playgroud)
尝试了类似下面的内容,但是它抛出错误
scala> a.scanLeft(scala.collection.mutable.Map[String,Int]()){ (x,t) => {x(t) = x(t)+1; (x) } }
java.util.NoSuchElementException: key not found: she
at scala.collection.MapLike$class.default(MapLike.scala:228)
at scala.collection.AbstractMap.default(Map.scala:59)
at scala.collection.mutable.HashMap.apply(HashMap.scala:65)
at $anonfun$1.apply(<console>:13)
at $anonfun$1.apply(<console>:13)
at scala.collection.TraversableLike$$anonfun$scanLeft$1.apply(TraversableLike.scala:374)
at scala.collection.TraversableLike$$anonfun$scanLeft$1.apply(TraversableLike.scala:374)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.TraversableLike$class.scanLeft(TraversableLike.scala:374)
at scala.collection.AbstractTraversable.scanLeft(Traversable.scala:104)
... 32 elided
scala>
Run Code Online (Sandbox Code Playgroud)
颇为费解的foldLeft似乎起作用。
a.foldLeft((List.empty[(String,Int)],Map[String,Int]().withDefaultValue(0))){
case ((lst,cnts),s) => ((s,cnts(s)+1) :: lst, cnts + ((s,cnts(s)+1)))
}._1.reverse
//res0: List[(String, Int)] = List((she,1), (sells,1), (seashells,1), (by,1), (the,1), (seashore,1), (the,2), (shells,1), (she,2), (sells,2), (are,1), (surely,1), (seashells,2), (where,1), (are,2), (the,3), (shells,2))
Run Code Online (Sandbox Code Playgroud)