Scala 斯卡拉,收双箱

Scala 斯卡拉,收双箱,scala,sum,case,collect,Scala,Sum,Case,Collect,嗨 如何改变这一点 for (w <- m ){ val w = (w._2.collect { case x if (x._2 > 0) => x._2; case x if (x._2 < 0) => x._2 }) // if I add here .sum i got sum of (negative and positive) together } 对于(w0)=>x.2; 案例x如果(x.\u 2x.\u 2})//如果

嗨 如何改变这一点

for (w <- m ){ val w = (w._2.collect { case x if (x._2 > 0) => x._2; case x if (x._2 < 0) => x._2 }) // if I add here .sum i got sum of (negative and positive) together }

对于(w0)=>x.2; 案例x如果(x.\u 2<0)=>x.\u 2})//如果我在这里加上。求和我得到了(负和正)的和 }
要在一个集合中获得正值和负值之和,可以是列表(positive.sum、negative.sum)或两个值

编辑: 仅限groupby、slice、collect、sum和屈服

我写了一个工作程序,但它没有被接受,因为它做了两次收集

val m = d.groupBy(_._1.slice(0, 7))
    for (w<- m) {
     val x = (w._2.collect { case x if (x._2>  0) =>  x._2 }).sum
     val y = (w._2.collect { case x if (x._2<  0) =>  x._2 }).sum
      println("%7s %11.2f %11.2f %11.2f" format(w._1 , x , y ,(x+y)))
    }
}

valm=d.groupBy(_._1.slice(0,7))
对于(w0)=>x.2})。求和
val y=(w._2.collect{case x if(x._2<0)=>x._2}).sum
println(“%7s%11.2f%11.2f%11.2f”格式(w._1,x,y,(x+y)))
}
}
输入数据为

val d = List(("2011-01-04", -137.76), ("2011-01-04", 2376.45), ("2011-01-04", -1.70), ("2011-01-04", -1.70), ("2011-01-04", -1.00), ("2011-01-06", 865.70), ("2011-01-07", -734.15), ("2011-01-05", -188.63), ("2011-01-06", -73.50), ("2011-01-07", -200.00), ("2011-01-09", -215.35), ("2011-01-09", -8.86), ("2011-01-09", -300.00), ("2011-01-11", -634.54), ("2011-01-11", -400.00), ("2011-01-12", -92.87), ("2011-01-13", -1839.24), ("2011-01-13", 10000.00), ("2011-01-13", -10000.00), ("2011-01-15", -127.97), ("2011-01-15", -319.02), ("2011-01-19", -549.00), ("2011-01-21", -164.80), ("2011-01-23", -500.00), ("2011-01-25", -377.97), ("2011-01-26", 2158.66), ("2011-01-26", -130.45), ("2011-01-27", -350.00), ("2011-01-29", -500.00), ("2011-02-01", 2376.45), ("2011-02-01", 955.00))

val d=列表((“2011-01-04”、-137.76), ("2011-01-04", 2376.45), ("2011-01-04", -1.70), ("2011-01-04", -1.70), ("2011-01-04", -1.00), ("2011-01-06", 865.70), ("2011-01-07", -734.15), ("2011-01-05", -188.63), ("2011-01-06", -73.50), ("2011-01-07", -200.00), ("2011-01-09", -215.35), ("2011-01-09", -8.86), ("2011-01-09", -300.00), ("2011-01-11", -634.54), ("2011-01-11", -400.00), ("2011-01-12", -92.87), ("2011-01-13", -1839.24), ("2011-01-13", 10000.00), ("2011-01-13", -10000.00), ("2011-01-15", -127.97), ("2011-01-15", -319.02), ("2011-01-19", -549.00), ("2011-01-21", -164.80), ("2011-01-23", -500.00), ("2011-01-25", -377.97), ("2011-01-26", 2158.66), ("2011-01-26", -130.45), ("2011-01-27", -350.00), ("2011-01-29", -500.00), ("2011-02-01", 2376.45), ("2011-02-01", 955.00))

使用递归和使用累加器的内部函数:

def sumIt(input:List[Tuple2[Int,Int]):Tuple2[Int,Int}={
    def sum_vals(entries:List[Tuple2[Int,Int]], acc:Tuple2[Int,Int]):Tuple2[Int,Int]={
        entries match{
            case x::xs => if(x._2 < 0){ 
                    sum_vals(xs, (acc._1, acc._2+x._2))
                } 
                else{ 
                    sum_vals(xs, (acc._1+x._2, acc._2))
                }
            case Nil => acc
        }
    }
    sum_vals(input, (0,0))
}
def sumIt(输入:List[Tuple2[Int,Int]):Tuple2[Int,Int}={
def sum_vals(条目:List[Tuple2[Int,Int]],acc:Tuple2[Int,Int]):Tuple2[Int,Int]={
条目匹配{
情形x::xs=>如果(x._2<0){
总价值(x,(附件1、附件2+x附件2))
} 
否则{
总价值(x,(附件1+x.附件2,附件2))
}
案例无=>acc
}
}
总和(输入,(0,0))
}
我假设您希望所有的负值都保存在返回的元组的第一项中,负值保存在第二项中

编辑:

FoldLeft,我需要从FoldLeft的角度思考:

def sum_vals(left:Tuple2[Int,Int], right:Tuple2[Int,Int])={
    if(right._2 < 0){
        (left._1, left._2 + right._2)
    }
    else{
        (left._1+right._2, left._2)
    }
 }

myCollection.foldLeft((0,0))( x,y => sum_vals(x,y) )
def sum_vals(左:Tuple2[Int,Int],右:Tuple2[Int,Int])={
如果(右)_2<0{
(左。_1,左。_2+右。_2)
}
否则{
(左。_1+右。_2,左。_2)
}
}
myCollection.foldLeft((0,0))(x,y=>sum_vals(x,y))

使用递归,内部函数使用累加器:

def sumIt(input:List[Tuple2[Int,Int]):Tuple2[Int,Int}={
    def sum_vals(entries:List[Tuple2[Int,Int]], acc:Tuple2[Int,Int]):Tuple2[Int,Int]={
        entries match{
            case x::xs => if(x._2 < 0){ 
                    sum_vals(xs, (acc._1, acc._2+x._2))
                } 
                else{ 
                    sum_vals(xs, (acc._1+x._2, acc._2))
                }
            case Nil => acc
        }
    }
    sum_vals(input, (0,0))
}
def sumIt(输入:List[Tuple2[Int,Int]):Tuple2[Int,Int}={
def sum_vals(条目:List[Tuple2[Int,Int]],acc:Tuple2[Int,Int]):Tuple2[Int,Int]={
条目匹配{
情形x::xs=>如果(x._2<0){
总价值(x,(附件1、附件2+x附件2))
} 
否则{
总价值(x,(附件1+x.附件2,附件2))
}
案例无=>acc
}
}
总和(输入,(0,0))
}
我假设您希望所有的负值都保存在返回的元组的第一项中,负值保存在第二项中

编辑:

FoldLeft,我需要从FoldLeft的角度思考:

def sum_vals(left:Tuple2[Int,Int], right:Tuple2[Int,Int])={
    if(right._2 < 0){
        (left._1, left._2 + right._2)
    }
    else{
        (left._1+right._2, left._2)
    }
 }

myCollection.foldLeft((0,0))( x,y => sum_vals(x,y) )
def sum_vals(左:Tuple2[Int,Int],右:Tuple2[Int,Int])={
如果(右)_2<0{
(左。_1,左。_2+右。_2)
}
否则{
(左。_1+右。_2,左。_2)
}
}
myCollection.foldLeft((0,0))(x,y=>sum_vals(x,y))

我认识这个家庭作业:)

因此,看起来m是一个
映射
,您不太关心输出中的键(可能此时您已经使用了filterKeys),因此可能最简单的方法是先取出值,然后过滤-避免所有那些繁琐的元组及其下划线

val values = m.values
val positives = values filter { _ >= 0 }
val negatives = values filter { _ < 0 }
或者甚至使用所谓的“无点”风格,但这可能会把它推得太远:

val (positives,negatives) = m.values partition { 0 < }
val(正、负)=m.values分区{0<}

你现在应该不会有任何问题,知道如何处理
积极的
消极的

我认识到这个家庭作业:)

因此,看起来m是一个
映射
,您不太关心输出中的键(可能此时您已经使用了filterKeys),因此可能最简单的方法是先取出值,然后过滤-避免所有那些繁琐的元组及其下划线

val values = m.values
val positives = values filter { _ >= 0 }
val negatives = values filter { _ < 0 }
或者甚至使用所谓的“无点”风格,但这可能会把它推得太远:

val (positives,negatives) = m.values partition { 0 < }
val(正、负)=m.values分区{0<}

您现在应该不会有任何问题,知道如何处理
正片
负片

我假设您使用的是像map[String,map[String,Int]]这样的地图地图。在这种情况下,解决方案可以如下所示:

val m = Map("hello" -> Map("a" -> 1, "b" -> -5, "c" -> 3, "d" -> -2))

val sums = m map {
  case (k, v) =>
    k -> (v.values partition (_ > 0) match {
      case (p, n) => (p.sum, n.sum)
    }).productIterator.toList
}

println(sums)
Map(hello -> List(4, -7))
for (w <- m) {
  val sums = (w._2.values.partition(_ > 0) match {
    case (p, n) => (p.sum, n.sum)
  }).productIterator.toList
}
输出结果如下所示:

val m = Map("hello" -> Map("a" -> 1, "b" -> -5, "c" -> 3, "d" -> -2))

val sums = m map {
  case (k, v) =>
    k -> (v.values partition (_ > 0) match {
      case (p, n) => (p.sum, n.sum)
    }).productIterator.toList
}

println(sums)
Map(hello -> List(4, -7))
for (w <- m) {
  val sums = (w._2.values.partition(_ > 0) match {
    case (p, n) => (p.sum, n.sum)
  }).productIterator.toList
}
如果您仍想使用,则可以如下所示:

val m = Map("hello" -> Map("a" -> 1, "b" -> -5, "c" -> 3, "d" -> -2))

val sums = m map {
  case (k, v) =>
    k -> (v.values partition (_ > 0) match {
      case (p, n) => (p.sum, n.sum)
    }).productIterator.toList
}

println(sums)
Map(hello -> List(4, -7))
for (w <- m) {
  val sums = (w._2.values.partition(_ > 0) match {
    case (p, n) => (p.sum, n.sum)
  }).productIterator.toList
}
(w 0)匹配的
{
案例(p,n)=>(p.sum,n.sum)
}).productIterator.toList
}

(使用Scala 2.8.1进行测试)

我假设您使用的是map-of-maps,比如map[String,map[String,Int]],在这种情况下,解决方案可以如下所示:

val m = Map("hello" -> Map("a" -> 1, "b" -> -5, "c" -> 3, "d" -> -2))

val sums = m map {
  case (k, v) =>
    k -> (v.values partition (_ > 0) match {
      case (p, n) => (p.sum, n.sum)
    }).productIterator.toList
}

println(sums)
Map(hello -> List(4, -7))
for (w <- m) {
  val sums = (w._2.values.partition(_ > 0) match {
    case (p, n) => (p.sum, n.sum)
  }).productIterator.toList
}
输出结果如下所示:

val m = Map("hello" -> Map("a" -> 1, "b" -> -5, "c" -> 3, "d" -> -2))

val sums = m map {
  case (k, v) =>
    k -> (v.values partition (_ > 0) match {
      case (p, n) => (p.sum, n.sum)
    }).productIterator.toList
}

println(sums)
Map(hello -> List(4, -7))
for (w <- m) {
  val sums = (w._2.values.partition(_ > 0) match {
    case (p, n) => (p.sum, n.sum)
  }).productIterator.toList
}
如果您仍想使用,则可以如下所示:

val m = Map("hello" -> Map("a" -> 1, "b" -> -5, "c" -> 3, "d" -> -2))

val sums = m map {
  case (k, v) =>
    k -> (v.values partition (_ > 0) match {
      case (p, n) => (p.sum, n.sum)
    }).productIterator.toList
}

println(sums)
Map(hello -> List(4, -7))
for (w <- m) {
  val sums = (w._2.values.partition(_ > 0) match {
    case (p, n) => (p.sum, n.sum)
  }).productIterator.toList
}
(w 0)匹配的
{
案例(p,n)=>(p.sum,n.sum)
}).productIterator.toList
}
(使用Scala 2.8.1测试)

以下是解决方案:

val m = for ((k, v) <- d.groupBy(_._1.slice(0, 7))) yield k -> (for ((t, nums) <- (for ((s, l) <- (for ((date, num) <- v) yield (num, if (num > 0) 'positive else 'negative)).groupBy(_._2)) yield s -> (for ((num, _) <- l) yield num))) yield t -> nums.sum)

for ((month, nums) <- m; positive <- nums.get('positive) orElse Some(0D); negative <- nums.get('negative) orElse Some(0D)) {
  println("%7s %11.2f %11.2f %11.2f" format(month, positive, negative, positive + negative))
}
valm=for((k,v)(for((t,nums)以下是解决方案:

val m = for ((k, v) <- d.groupBy(_._1.slice(0, 7))) yield k -> (for ((t, nums) <- (for ((s, l) <- (for ((date, num) <- v) yield (num, if (num > 0) 'positive else 'negative)).groupBy(_._2)) yield s -> (for ((num, _) <- l) yield num))) yield t -> nums.sum)

for ((month, nums) <- m; positive <- nums.get('positive) orElse Some(0D); negative <- nums.get('negative) orElse Some(0D)) {
  println("%7s %11.2f %11.2f %11.2f" format(month, positive, negative, positive + negative))
}

val m=for(k,v)(for(t,nums)也许你是对的,但我需要修改这个,我认为带折叠的解决方案的收益率+1。尽管它肯定可以写得更简洁。也许你是对的,但我需要修改这个,我认为带折叠的解决方案的收益率+1。尽管它肯定可以写得更简洁。没有任何论据!-1对所有的人