scala deal with string and do accumulative -


i have variable:

val list= rows.sortby(- _._2).map{case (user , list) => list}.take(20).mkstring("::")   

the result println(list) should be:

60::58::51::48::47::47::45::45::43::43::42::42::42::41::41::41::40::40::40::39 

and have deal these numbers (like histogram concept)

if set break 10, should divide max number (60) 10 , make 6 buckets:

  • the scope between 0~ 10(0<x<=10) have 0 numbers match
  • the scope between 10~ 20(10<x<=20) have 0 numbers match
  • the scope between 20~ 30(20<x<=30) have 0 numbers match
  • the scope between 30~ 40(30<x<=40) have 4 numbers match
  • the scope between 40~ 50(40<x<=50) have 13 numbers match
  • the scope between 50~ 60(50<x<=60) have 3 numbers match

and have save 2 variables x , y :

x:  0~10::10~20::20~30::30~40::40~50::50~60 y:  0::0::0::4::13::3 

how can this?

val actuallist = list.split("::").map(_.toint).tolist val step = 10 val steps = step actuallist.max step //for each step, output count of items in list  //between current step , currentstep - stepval val counts = steps.map(x=>actuallist.count(y=>y <= x && y > x - step)) val stepsasstring = steps.map(x=>s"${x-step}~$x") 

and can map them too:

steps.zip(counts).tomap 

note made more performant if list sorted first, wouldn't worry tuning unless need it


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -