简体   繁体   中英

Divide two Arrays based on key

I am trying to calculate the average length of words starting with each letter.

I have managed to get the length of all the words beginning with each letter.

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))

I need to divide letterMapCount with wordMapCount but can't figure out how to do it.

My output should be similar to this format:

a 2.211
b 3.28
c 5.631
...
z 9.412

Can anyone provide some guidance as to how I can do this?

Assuming the two arrays have the same order and same number of entries (which seems to be the case):

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

If this is clearer:

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) }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM