scala groupby和Observable的总和

时间:2015-05-25 18:09:20

标签: scala

我是scala初学者,我希望在groupby上执行简单的sumObservable。例如:

val test = Observable.just(("a", 1), ("a", 2), ("b", 5), ("b",3))

我想按键分组并对值进行求和,以便得到类似的内容:

(a,3)
(b,8)

我可以将所有内容与test.map(_._2).sum相加,但不能在执行groupby

2 个答案:

答案 0 :(得分:2)

不是你正在寻找的课程。我在scala.react和reactivex.io周围翻找,但是这个怎么样:

scala> val test999=Seq(("a",1),("a",16),("b",5),("a",9),("b",9),("c",90))
test999: Seq[(String, Int)] = List((a,1), (a,16), (b,5), (a,9), (b,9), (c,90))

scala> test999
res12: Seq[(String, Int)] = List((a,1), (a,16), (b,5), (a,9), (b,9), (c,90))

scala> test999.groupBy(_._1).mapValues(_.map(_._2).sum)
res13: scala.collection.immutable.Map[String,Int] = Map(b -> 14, a -> 26, c -> 90)

答案 1 :(得分:0)

鉴于此

  

我可以使用test.map(_._ 2).sum,...

来总结所有内容

即可以map test groupBy,考虑将test.toSeq应用于 2015-05-26 08:13:05,515 6260 ERROR odoodb1 openerp.addons.base.ir.ir_ui_view: Field `titre` does not exist Error context: View `document.form` [view_id: 873, xml_id: document_binov.document_form, model: document_binov.document_binov, parent_id: n/a] 2015-05-26 08:13:05,535 6260 INFO odoodb1 werkzeug: 127.0.0.1 - - [26/May/2015 08:13:05] "GET / HTTP/1.1" 500 - 2015-05-26 08:13:05,588 6260 ERROR odoodb1 werkzeug: Error on request: raise ValidationError('\n'.join(errors)) ParseError: "ValidateError Field(s) `arch` failed against a constraint: Invalid view definition Error details: Field `titre` does not exist Error context: View `document.form` [view_id: 873, xml_id: document_binov.document_form, model: document_binov.document_binov, parent_id: n/a]" while parsing /home/binov1/git/odoo/addons/document_binov/templates.xml:29, near <record model="ir.ui.view" id="document_form"> <field name="name">document.form</field> <field name="model">document_binov.document_binov</field> <field name="type">form</field> <field name="arch" type="xml"> <form string="Documents"> <field name="titre"/> <field name="description"/> <field name="type"/> </form> </field> </record>