I did a script, part of it is to update a list within a loop. foreach loop can not update the list, but for loop can. Here is the sample code:
For loop:
var Typea = scala.collection.mutable.ListBuffer[String]()
var Typeb = scala.collection.mutable.ListBuffer[String]()
val Pattern1 = "TYPEA".r
val Pattern2 = "TYPEB".r
var rrr=rdd.toDF.collect()
for (f<-rrr) {
val fName=f.getString(0)
for (k<- fName.split("\\/")) { if ( Pattern1.findFirstIn(k)!=None)
{println(k); Typea:+=k.toString}
else if ( Pattern2.findFirstIn(k)!=None)
{println(k); Typeb:+=k.toString}
}
}
val Typea1=Typea.distinct.sorted
val Typeb1=Typeb.distinct.sorted
println(Typea)
println(Typeb)
println(Typea1)
println(Typeb1)
It updated the list, Typea, Typeb. After updating, I printed the lists, there were strings in it.
Foreach loop:
var Typea = scala.collection.mutable.ListBuffer[String]()
var Typeb = scala.collection.mutable.ListBuffer[String]()
val Pattern1 = "TYPEA".r
val Pattern2 = "TYPEB".r
rdd.toDF.foreach{ f=>
val fName=f.getString(0)
fName.split("\\/").foreach(k=> if ( Pattern1.findFirstIn(k)!=None)
{println(k); Typea:+=k.toString; println(Typea)}
else if ( Pattern2.findFirstIn(k)!=None)
{println(k); Typeb:+=k.toString;println(Typeb)}
)
}
val Typea1=Typea.distinct.sorted
val Typeb1=Typeb.distinct.sorted
println(Typea)
println(Typeb)
println(Typea1)
println(Typeb1)
After updating the list, I printed the lists, they were empty.
I wrote about the solutions to some problems I found from programming and data analytics. They may help you on your work. Thank you.
ezoic
Subscribe to:
Post Comments (Atom)
looking for a man
I am a mid aged woman. I was born in 1980. I do not have any kid. no complicated dating before . I am looking for a man here for marriage...
-
I tried to commit script to bitbucket using sourcetree. I first cloned from bitbucket using SSH, and I got an error, "authentication ...
-
https://github.com/boto/boto3/issues/134 import boto3 import botocore client = boto3.client('s3') result = client.list_obje...
-
There are some fun tools on Mac/PC which can help you on your studies, life and research. 1. Evernote: https://evernote.com/ To downl...
No comments:
Post a Comment