val input=Set(Set("a","b"),Set("b","c"))
我要这个:
Map("a"->1,"b"->2,"c"->1)
实现此类功能的最佳功能方法是什么?
在嵌套的 Iterables 中使用 yield 关键字结果:
output = for(firstlevel<-input) yield for(item<-firstlevel) yield item
最佳答案
更新:纳入了使用建议 input.toSeq.flatten
而不是 input.toSeq flatMap { _.toSeq }
转换为单个值序列...
input.toSeq.flatten
...匹配的组值...
input.toSeq.flatten groupBy { identity }
...并数数
input.toSeq.flatten groupBy { identity } mapValues { _.size }
关于scala - 处理 Set of Sets 并返回一个扁平的 Iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4746610/