我对scala和高级编程相对较新,所以我的一些概念是有线连接的。 我使用casbah从mongo db获取数据,我得到的数据是下面提到的项目列表。
val m: List[(AnyRef, java.util.Date, AnyRef, AnyRef, AnyRef, AnyRef, AnyRef, AnyRef, Double, Double, Double, Double, AnyRef, AnyRef, Double, Double, Double, Double, Double, Double, String)]
但是当我试图操纵列表并向其添加一列时,该变量将变为List [List [java.io.Serializable]]。
是否有可能获得扁平列表而不是List [List [java.io.Serializable]]。 我想得到像
这样的东西列表[(AnyRef,java.util.Date,AnyRef,AnyRef,AnyRef,AnyRef,AnyRef,AnyRef,Double,Double,Double,Double,AnyRef,AnyRef,Double,Double,Double,Double,Double,Double,串,的 双 )]
非常感谢任何帮助。
m filter {x => x._9 > 0} map {x => x::fmt(((x._9-x._10)/(x._9))*100)::Nil }
//> res2: List[List[java.io.Serializable]] = List(List((A:NYQ,Tue Dec 03 00:00:
//| 00 GMT 26,USA,NYQ,Agilent Technologies Inc,Electronic-and-Electrical-Equipm
//| ent,27-03-2015,22-04-2015,41.11,40.6,35.61,42.99,USD,USD,2.6,0.01,1.044,0.0
//| 95,0.119,33.44,2014), 1.241), List((AA:NYQ,Tue Dec 03 00:00:00 GMT 26,USA,N
//| YQ,Alcoa Inc,Industrial-Metals,04-02-2015,25-02-2015,12.7,12.88,12.12,17.75
//| ,USD,USD,1.26,0.009,2.039,0.014,0.043,63.8,2014), -1.417), List((AAPL:NSQ,T
//| ue Dec 03 00:00:00 GMT 26,USA,NSQ,Apple Inc,Technology-Hardware-and-Equipme
//| nt,05-02-2015,12-02-2015,123.25,124.24,73.05,133.6,USD,USD,6.48,0.015,1.078
//| ,0.354,0.287,16.74,2014), -0.803), List((ABBV:NYQ,Tue Dec 03 00:00:00 GMT 2
//| 6,USA,NYQ,AbbVie Inc,Pharmaceuticals-and-Biotechnology,13-01-2015,13-02-201
//| 5,57.65,57.27,45.5,70.76,USD,USD,52.65,0.036,14.813,1.018,0.171,53.01,2014)
//| , 0.659), List((ABC:NYQ,Tue Dec 03 00:00:00 GMT 26,USA,NYQ,AmerisourceBerge
//| n Corp,Food-and-Drug-Re
//| Output exceeds cutoff limit.
扫描几个casbah文档和github页面。在类和函数中填充信息以填充地图内的信息要容易得多。这是工作。感谢堆栈溢出一如既往的帮助。
val m = for (d <- price.find(query)) yield(d) //> m : Iterator[com.mongodb.casbah.Imports.DBObject] = non-empty iterator
case class Stocks (val Symbol : String,
val Close : Double,
val PreviousClose : Double,
val DebtToEquity : Double,
val DailyChange : Double
)
def objectRead(o: DBObject): Stocks = {
Stocks(
Symbol = o.as[String]("Symbol"),
Close = fmt((o.getAs[DBObject]("PricesandVolume").get)("Close")).toDouble,
PreviousClose = fmt((o.getAs[DBObject]("PricesandVolume").get)("PreviousClose")).toDouble,
DebtToEquity = fmt((o.getAs[DBObject]("Ratios").get)("DebtToEquity")).toDouble,
DailyChange = fmt((((fmt((o.getAs[DBObject]("PricesandVolume").get)("Close")).toDouble - fmt((o.getAs[DBObject]("PricesandVolume").get)("PreviousClose")).toDouble)/fmt((o.getAs[DBObject]("PricesandVolume").get)("Close")).toDouble))*100).toDouble
)
} //> objectRead: (o: com.mongodb.casbah.Imports.DBObject)cashbahtest.Stocks
val a = (m map {x => objectRead(x)}).toList.sortBy(_.DailyChange)
//> a : List[cashbahtest.Stocks] = List(Stocks(RIG:NYQ,14.49,15.33,1.078,-5.79
//| 7), Stocks(CCJ:NYQ,14.0,14.75,0.556,-5.357), Stocks(MIL:NYQ,3.92,4.13,0.884
//| ,-5.357), Stocks(DNR:NYQ,7.52,7.92,1.231,-5.319), Stocks(PGH:NYQ,3.01,3.17,
//| 1.108,-5.316), Stocks(TC:NYQ,1.34,1.41,2.206,-5.224), Stocks(SDRL:NYQ,9.64,
//| 10.06,1.715,-4.357), Stocks(PWE:NYQ,1.66,1.73,0.765,-4.217), Stocks(PT:NYQ,
//| 0.66,0.687,6.325,-4.091), Stocks(TCK:NYQ,13.86,14.42,0.98,-4.04), Stocks(ER
//| F:NYQ,10.09,10.49,0.905,-3.964), Stocks(PDS:NYQ,6.21,6.45,1.175,-3.865), St
//| ocks(FCX:NYQ,18.8,19.5,2.215,-3.723), Stocks(ATE:NYQ,12.79,13.26,0.977,-3.6
//| 75), Stocks(ECA:NYQ,10.93,11.25,1.542,-2.928), Stocks(QEP:NYQ,21.07,21.68,1
//| .279,-2.895), Stocks(EMC:NYQ,25.17,25.81,1.096,-2.543), Stocks(SNDK:NSQ,64.
//| 59,66.2,0.576,-2.493), Stocks(NBR:NYQ,13.3,13.63,1.42,-2.481), Stocks(POT:N
//| YQ,32.03,32.82,1.016,-2.466), Stocks(NE:NYQ,14.32,14.67,1.024,-2.444), Stoc
//| ks(VLO:NYQ,61.87,63.38,
//| Output exceeds cutoff limit.
答案 0 :(得分:1)
我认为这是因为您在地图步骤中使用了一个列表运算符来表示元组。因此,当您对每个元组进行追加时,实际上是将其强制转换为列表,这样您最终会得到一个列表列表。
有关在Scala中附加元组的信息,请参阅this other thread。