scala-将值映射到理解转换错误
我试图用转换链式映射()以便于理解,但出现错误scala-将值映射到理解转换错误,scala,Scala,我试图用转换链式映射()以便于理解,但出现错误 println("Citi names concatenated by country code") println("Method1") orec.groupBy( x => x.ccode ).mapValues( x => x.map( a => a.cityname).mkString("[",",","]")).take(5).foreach(println) println("Method2") 下面的代码抛出错误
println("Citi names concatenated by country code")
println("Method1")
orec.groupBy( x => x.ccode ).mapValues( x => x.map( a => a.cityname).mkString("[",",","]")).take(5).foreach(println)
println("Method2")
下面的代码抛出错误
var i = 0
for{
(key,value) <- orec.groupBy( x => x.ccode )
cities = value.map( a=> a.cityname)
cities2 = cities.mkString("[",",","]")
i=i+1
if(i<5) {
_ = println(key + "->" + cities2)
}
} yield ()
var i=0
为了{
(键,值)x.ccode)
cities=value.map(a=>a.cityname)
cities2=cities.mkString(“[”,“,”,“]”)
i=i+1
如果(i要替换你的take(5)
,你可以在索引上使用if,要生成索引,你可以使用zipWithIndex
如果要在scala中增加var
,则需要指定var的类型。(此处Int
)
这就是您可能要查找的代码
case class City(ccode: String, cityname: String)
val orec: Seq[City] = Seq(City("fr", "paris"), City("fr", "lille"), City("fr", "bordeaux"), City("en", "london"), City("be", "gantt"), City("de", "berlin"), City("austria", "vienna"), City("us", "ny"))
for (((key, value), index) <- orec.groupBy( x => x.ccode ).zipWithIndex){
val cities = value.map( a=> a.cityname)
val cities2 = cities.mkString("[",",","]")
if (index<5)
println(key + "->" + cities2)
}
案例类城市(ccode:String,cityname:String)
val orec:Seq[城市]=Seq(城市(“法国”、“巴黎”)、城市(“法国”、“里尔”)、城市(“法国”、“波尔多”)、城市(“伦敦”)、城市(“be”、“甘特”)、城市(“德”、“柏林”)、城市(“奥地利”、“维也纳”)、城市(“美国”、“纽约”))
对于((键,值),索引)x.ccode.zipWithIndex){
val cities=value.map(a=>a.cityname)
val cities2=cities.mkString(“[”,“,”,“]))
如果(索引要替换您的take(5)
,您可以在索引上使用if来生成索引,您可以使用zipWithIndex
如果要在scala中增加var
,则需要指定var的类型。(此处Int
)
这就是您可能要查找的代码
case class City(ccode: String, cityname: String)
val orec: Seq[City] = Seq(City("fr", "paris"), City("fr", "lille"), City("fr", "bordeaux"), City("en", "london"), City("be", "gantt"), City("de", "berlin"), City("austria", "vienna"), City("us", "ny"))
for (((key, value), index) <- orec.groupBy( x => x.ccode ).zipWithIndex){
val cities = value.map( a=> a.cityname)
val cities2 = cities.mkString("[",",","]")
if (index<5)
println(key + "->" + cities2)
}
案例类城市(ccode:String,cityname:String)
val orec:Seq[城市]=Seq(城市(“法国”、“巴黎”)、城市(“法国”、“里尔”)、城市(“法国”、“波尔多”)、城市(“伦敦”)、城市(“be”、“甘特”)、城市(“德”、“柏林”)、城市(“奥地利”、“维也纳”)、城市(“美国”、“纽约”))
对于((键,值),索引)x.ccode.zipWithIndex){
val cities=value.map(a=>a.cityname)
val cities2=cities.mkString(“[”,“,”,“]))
如果(索引您将“中游分配”误认为是分配给您的var i
用于要求val
的语法:
scala> for (i <- List(42) ; val j = i + 1) yield j
^
warning: `val` keyword in for comprehension is deprecated: instead, bind the value without `val`
res3: List[Int] = List(43)
scala> val i = i + 1
^
error: recursive value i needs type
或
scala>for{
|(键,值)[巴黎,里尔,波尔多],美国->[纽约],德->[柏林],英->[伦敦],贝->[甘特],奥地利->[维也纳])
scala>.take(5).foreach{case(k,v)=>println(s“$k->$v”)}
fr->[巴黎、里尔、波尔多]
美国->[ny]
de->[柏林]
en->[伦敦]
be->[甘特]
或
scala>val it=orec.groupBy(u.ccode).mapValues(u.map(u.cityname).mkString(“[”,“,”,“,”])。迭代器
^
警告:trait MapOps中的方法mapValues已被弃用(从2.13.0开始):使用.view.mapValues(f)。将来的版本将包含此方法的严格版本(目前,.view.mapValues(f).toMap)。
它:迭代器[(字符串,字符串)]=
(k,v)[巴黎、里尔、波尔多]
美国->[ny]
de->[柏林]
en->[伦敦]
be->[甘特]
您将“中游分配”误认为是分配给您的var i
用于要求val
的语法:
scala> for (i <- List(42) ; val j = i + 1) yield j
^
warning: `val` keyword in for comprehension is deprecated: instead, bind the value without `val`
res3: List[Int] = List(43)
scala> val i = i + 1
^
error: recursive value i needs type
或
scala>for{
|(键,值)[巴黎,里尔,波尔多],美国->[纽约],德->[柏林],英->[伦敦],贝->[甘特],奥地利->[维也纳])
scala>.take(5).foreach{case(k,v)=>println(s“$k->$v”)}
fr->[巴黎、里尔、波尔多]
美国->[ny]
de->[柏林]
en->[伦敦]
be->[甘特]
或
scala>val it=orec.groupBy(u.ccode).mapValues(u.map(u.cityname).mkString(“[”,“,”,“,”])。迭代器
^
警告:trait MapOps中的方法mapValues已被弃用(从2.13.0开始):使用.view.mapValues(f)。将来的版本将包含此方法的严格版本(目前,.view.mapValues(f).toMap)。
它:迭代器[(字符串,字符串)]=
(k,v)[巴黎、里尔、波尔多]
美国->[ny]
de->[柏林]
en->[伦敦]
be->[甘特]
我知道这与问题无关,但你为什么要这样做?另外,你能给orec提供一个样本吗?这将有助于回答这个问题:)作为一个粗略的猜测,我认为你应该键入I。你为什么要“翻译”首先是它???XY问题?我知道这与问题无关,但你为什么要这样做?另外,你能给orec一个样本吗?这将有助于回答这个问题:)作为一个粗略的猜测,我会说你应该键入I。你为什么要“翻译”它???XY问题?
scala> for {
| (key, value) <- orec.groupBy(_.ccode)
| } yield (key, value.map(_.cityname).mkString("[",",","]"))
res4: scala.collection.immutable.Map[String,String] = HashMap(fr -> [paris,lille,bordeaux], us -> [ny], de -> [berlin], en -> [london], be -> [gantt], austria -> [vienna])
scala> .take(5).foreach { case (k,v) => println(s"$k -> $v") }
fr -> [paris,lille,bordeaux]
us -> [ny]
de -> [berlin]
en -> [london]
be -> [gantt]
scala> val it = orec.groupBy(_.ccode).mapValues(_.map(_.cityname).mkString("[",",","]")).iterator
^
warning: method mapValues in trait MapOps is deprecated (since 2.13.0): Use .view.mapValues(f). A future version will include a strict version of this method (for now, .view.mapValues(f).toMap).
it: Iterator[(String, String)] = <iterator>
scala> for ((k, v) <- it.take(5)) println(s"$k -> $v")
fr -> [paris,lille,bordeaux]
us -> [ny]
de -> [berlin]
en -> [london]
be -> [gantt]