1

我在输入中有以下列表:

val listInput1 = 
  List(
    "itemA,CATs,2,4",
    "itemA,CATS,3,1",
    "itemB,CATQ,4,5",
    "itemB,CATQ,4,6",
    "itemC,CARC,5,10")

我想在scala中使用groupBy和foldleft(只有一个函数)编写一个函数,以便为具有相同标题的行(这里的第一列)总结第三和第四列,想要的输出是:

val listOutput1 = 
      List(
         "itemA,CATS,5,5",
         "itemB,CATQ,8,11",
         "itemC,CARC,5,10"

       )


 def sumIndex (listIn:List[String]):List[String]={

 listIn.map(_.split(",")).groupBy(_(0)).map{ 
  case (title, label) => 
       "%s,%s,%d,%d".format(
         title,
         label.head.apply(1),
         label.map(_(2).toInt).sum,
         label.map(_(3).toInt).sum)}.toList

}

亲切的问候

4

3 回答 3

0

如果你有一个列表

val listInput1 =
  List(
    "itemA,CATs,2,4",
    "itemA,CATS,3,1",
    "itemB,CATQ,4,5",
    "itemB,CATQ,4,6",
    "itemC,CARC,5,10")

然后你可以编写一个通用函数,可以和foldLeftand reduceLeftas一起使用

def accumulateLeft(x: Map[String, Tuple3[String, Int, Int]], y: Map[String, Tuple3[String, Int, Int]]): Map[String, Tuple3[String, Int, Int]] ={
  val key = y.keySet.toList(0)
  if(x.keySet.contains(key)){
    val oldTuple = x(key)
    x.updated(key, (y(key)._1, oldTuple._2+y(key)._2, oldTuple._3+y(key)._3))
  }
  else{
    x.updated(key, (y(key)._1, y(key)._2, y(key)._3))
  }
}

你可以称它们为

向左折叠

listInput1
  .map(_.split(","))
  .map(array => Map(array(0) -> (array(1), array(2).toInt, array(3).toInt)))
  .foldLeft(Map.empty[String, Tuple3[String, Int, Int]])(accumulateLeft)
  .map(x => x._1+","+x._2._1+","+x._2._2+","+x._2._3)
  .toList
//res0: List[String] = List(itemA,CATS,5,5, itemB,CATQ,8,11, itemC,CARC,5,10)

减少左

listInput1
  .map(_.split(","))
  .map(array => Map(array(0) -> (array(1), array(2).toInt, array(3).toInt)))
  .reduceLeft(accumulateLeft)
  .map(x => x._1+","+x._2._1+","+x._2._2+","+x._2._3)
  .toList
//res1: List[String] = List(itemA,CATS,5,5, itemB,CATQ,8,11, itemC,CARC,5,10)

同样,您可以交换通用函数中的变量,以便它可以与foldRightreduceRight一起使用

def accumulateRight(y: Map[String, Tuple3[String, Int, Int]], x: Map[String, Tuple3[String, Int, Int]]): Map[String, Tuple3[String, Int, Int]] ={
  val key = y.keySet.toList(0)
  if(x.keySet.contains(key)){
    val oldTuple = x(key)
    x.updated(key, (y(key)._1, oldTuple._2+y(key)._2, oldTuple._3+y(key)._3))
  }
  else{
    x.updated(key, (y(key)._1, y(key)._2, y(key)._3))
  }
}

并调用该函数会给你

右折

listInput1
  .map(_.split(","))
  .map(array => Map(array(0) -> (array(1), array(2).toInt, array(3).toInt)))
  .foldRight(Map.empty[String, Tuple3[String, Int, Int]])(accumulateRight)
  .map(x => x._1+","+x._2._1+","+x._2._2+","+x._2._3)
  .toList
//res2: List[String] = List(itemC,CARC,5,10, itemB,CATQ,8,11, itemA,CATs,5,5)

减少权利

listInput1
  .map(_.split(","))
  .map(array => Map(array(0) -> (array(1), array(2).toInt, array(3).toInt)))
  .reduceRight(accumulateRight)
  .map(x => x._1+","+x._2._1+","+x._2._2+","+x._2._3)
  .toList
//res3: List[String] = List(itemC,CARC,5,10, itemB,CATQ,8,11, itemA,CATs,5,5)

因此,您实际上并不需要 agroupBy并且可以使用任何foldLeftfoldRight或函数来获得所需的输出。reduceLeftreduceRight

于 2018-05-26T05:26:58.603 回答
0

The logic in your code looks sound, here it is with a case class implemented as that handles edge cases more cleanly:

// represents a 'row' in the original list
case class Item(
                 name: String,
                 category: String,
                 amount: Int,
                 price: Int
               )

// safely converts the row of strings into case class, throws exception otherwise
def stringsToItem(strings: Array[String]): Item = {
  if (strings.length != 4) {
    throw new Exception(s"Invalid row: ${strings.foreach(print)}; must contain only 4 entries!")
  } else {
    val n = strings.headOption.getOrElse("N/A")
    val cat = strings.lift(1).getOrElse("N/A")
    val amt = strings.lift(2).filter(_.matches("^[0-9]*$")).map(_.toInt).getOrElse(0)
    val p = strings.lastOption.filter(_.matches("^[0-9]*$")).map(_.toInt).getOrElse(0)

    Item(n, cat, amt, p)
  }
}

// original code with case class and method above used
listInput1.map(_.split(","))
  .map(stringsToItem)
  .groupBy(_.name)
  .map { case (name, items) =>
    Item(
      name,
      category = items.head.category,
      amount = items.map(_.amount).sum,
      price = items.map(_.price).sum
    )
  }.toList
于 2018-05-26T05:37:35.557 回答
0

你可以用一个 foldLeft 解决它,只迭代输入列表一次。使用地图聚合结果。

listInput1.map(_.split(",")).foldLeft(Map.empty[String, Int]) {
  (acc: Map[String, Int], curr: Array[String]) =>
    val label: String = curr(0)
    val oldValue: Int = acc.getOrElse(label, 0)
    val newValue: Int = oldValue + curr(2).toInt + curr(3).toInt
    acc.updated(label, newValue)
}

结果:地图(itemA -> 10,itemB -> 19,itemC -> 15)

于 2018-05-25T21:38:10.410 回答