根据键划分两个数组

时间:2016-10-09 12:06:08

标签: scala

我正在尝试计算以每个字母开头的单词的平均长度。

我设法得到每个字母开头的所有单词的长度。

val wordMapCount

Array[(String, Int)] = Array((d,30168), (s,66277),(p,28006), (x,14), (e,19273), (w,59977), (z,73), (a,85621), (t,124595), (i,62296), (b,45909), (k,9527), (u,9221), (h,60919), (y,25896), (o,43646), (n,26979), (f,37123), (q,2388), (j,3361), (v,5788), (g,20983), (l,29854), (r,14473), (m,56165), (c,34864))

val letterMapCount

Array[(String, Int)] = Array((d,155127), (s,330535), (p,177717), (x,38), (e,108830), (w,266442), (z,366), (a,280953), (t,476273), (i,142096), (b,203263), (k,45228), (u,42140), (h,242073), (2,376), (y,91801),(o,124698), (n,102174), (f,180722), (q,14144), (j,17261), (v,34619), (g,108461), (l,140429), (r,86426), (m,224474), (c,220884))

我需要将letterMapCountwordMapCount分开,但无法弄明白该怎么做。

我的输出应该与此格式类似:

a 2.211
b 3.28
c 5.631
...
z 9.412

任何人都可以就如何做到这一点提供一些指导吗?

2 个答案:

答案 0 :(得分:0)

假设两个数组具有相同的顺序和相同数量的条目(似乎是这种情况):

val result: Array[(String, Double)] = wordMapCount
  .zip(letterMapCount)
  .map { case ((letter, wc), (_, lc)) => (letter, lc.toDouble / wc) }
  .sortBy(_._1)

答案 1 :(得分:0)

  val a: Array[(String, Int)] = Array(("d",155127), ("s",330535), ("p",177717))
  val b: Array[(String, Int)] = Array(("d",30168), ("s",66277),("p",28006))
  val res: Array[(String, Double)] = a.map { x => (x._1, b.find(y => y._1 == x._1).getOrElse(("None",0))._2*1.0/x._2) }
  // res = Array((d,0.19447291574000658), (s,0.20051431769706687), (p,0.15758762526938896))


// find returns Option[(String, Double)]
// define orElse in order to be able to take ._2 from .find result
// take the found value form b and multiply it by 1.0 to turn it to a double
// divide it by the value in a

如果更清楚:

a.map { a_value => (a_value._1, b.find(b_value => b_value._1 == a_value._1).getOrElse(("None",0))._2*1.0/a_value._2) }