我在txt文件中有一个外部列表,我需要抓住第一个字符串并将其用作键,这很好用,然后我需要一个数字列表。但是我只得到第一个,我做错了什么? 因此,当我需要将其作为完整列表而不仅仅是第一个数字时,当前输出将是SK1,9-SK2,0等。 我在Intelije上使用Scala
/**
* Created by Andre on 10/11/2016.
*/
import scala.io.Source
import scala.io.StdIn.readInt
import scala.io.StdIn.readLine
import scala.collection.immutable.ListMap
object StockMarket extends App{
// APPLICATION LOGIC
// reads the data from text file
val mapdata = readFile("data.txt")
// print data to check it's been read in correctly
println(mapdata)
// *******************************************************************************************************************
// UTILITY FUNCTIONS
// reads data file - comma separated file
def readFile(filename: String): Map[String, Int] = {
// create buffer to build up map as we read each line
var mapBuffer: Map[String, Int] = Map()
try {
for (line <- Source.fromFile(filename).getLines()) { // for each line
val splitline = line.split(",").map(_.trim).toList // split line at , and convert to List
// add element to map buffer
// splitline is line from file as List, e.g. List(Bayern Munich, 24)
// use head as key
// tail is a list, but need just the first (only in this case) element, so use head of tail and convert to int
mapBuffer = mapBuffer ++ Map(splitline.head -> splitline.tail.head.toInt)
}
} catch {
case ex: Exception => println("Sorry, an exception happened.")
}
mapBuffer
}
}
我的外部名单
SK1, 9, 7, 2, 0, 7, 3, 7, 9, 1, 2, 8, 1, 9, 6, 5, 3, 2, 2, 7, 2, 8, 5, 4, 5, 1, 6, 5, 2, 4, 1
SK2, 0, 7, 6, 3, 3, 3, 1, 6, 9, 2, 9, 7, 8, 7, 3, 6, 3, 5, 5, 2, 9, 7, 3, 4, 6, 3, 4, 3, 4, 1
SK4, 2, 9, 5, 7, 0, 8, 6, 6, 7, 9, 0, 1, 3, 1, 6, 0, 0, 1, 3, 8, 5, 4, 0, 9, 7, 1, 4, 5, 2, 8
SK5, 2, 6, 8, 0, 3, 5, 5, 2, 5, 9, 4, 5, 3, 5, 7, 8, 8, 2, 5, 9, 3, 8, 6, 7, 8, 7, 4, 1, 2, 3
SK6, 2, 7, 5, 9, 1, 9, 8, 4, 1, 7, 3, 7, 0, 8, 4, 5, 9, 2, 4, 4, 8, 7, 9, 2, 2, 7, 9, 1, 6, 9
SK7, 6, 9, 5, 0, 0, 0, 0, 5, 8, 3, 8, 7, 1, 9, 6, 1, 5, 3, 4, 7, 9, 5, 5, 9, 1, 4, 4, 0, 2, 0
SK8, 2, 8, 8, 3, 1, 1, 0, 8, 5, 9, 0, 3, 1, 6, 8, 7, 9, 6, 7, 7, 0, 9, 5, 2, 5, 0, 2, 1, 8, 6
SK9, 7, 1, 8, 8, 4, 4, 2, 2, 7, 4, 0, 6, 9, 5, 5, 4, 9, 1, 8, 6, 3, 4, 8, 2, 7, 9, 7, 2, 6, 6
答案 0 :(得分:1)
这是您的代码,只有很少的更改:
// I split it on two functions just to facilitate testing:
def readFile(filename: String): Map[String, List[Int]] = {
processInput(Source.fromFile(filename).getLines)
}
def processInput(lines: Iterator[String]): Map[String, List[Int]] = {
var mapBuffer: Map[String, List[Int]] = Map()
try {
for (line <- lines) {
val splitline = line.split(",").map(_.trim).toList
// here instead of taking .tail.head, we map over the tail (all numbers):
mapBuffer = mapBuffer + (splitline.head -> splitline.tail.map(_.toInt))
}
} catch {
case ex: Exception => println("Sorry, an exception happened.")
}
mapBuffer
}
这是一个解决方案,我相信,它更像是一个惯用的Scala代码:
import scala.util.Try
def processInput(lines: Iterator[String]): Map[String, List[Int]] = {
Try {
lines.foldLeft( Map[String, List[Int]]() ) { (acc, line) =>
val splitline = line.split(",").map(_.trim).toList
acc.updated(splitline.head, splitline.tail.map(_.toInt))
}
}.getOrElse {
println("Sorry, an exception happened.")
Map()
}
}
差异主要是
var
Map
(顺便说一下,你不需要var
来变异
它)foldLeft
迭代并累积Map
而不是for
scala.util.Try
而不是试试。