如何使用列表映射

时间:2017-11-15 21:34:00

标签: scala

所以我有一个population.csv文件,看起来像这样:

country,population_size
Afghanistan,32758020
Albania,2889104
Algeria,39113313
American Samoa,55437
Andorra,79223
Angola,26920466
Antigua and Barbuda,98875
Argentina,42981515

我使用此

逐行分隔它们
val file_population = "population.csv"
def get_csv_file(file: String) : List[String] =
{
    val body = scala.io.Source.fromFile(file_population).mkString
    body.split("\n").toList
}

之后我需要根据逗号分割字符串。这将生成从国家/地区名称到人口规模的地图。

我尝试过,目前有以下内容:

def process_pops(lines: List[String]) : Map[String, Long] = 
{
    val seperated = for ( line <- lines ) yield (line.split(",").toList(0), (line.split(",").toList(1)).toLong )
    for ( i <- seperated)
    {
        val map = Map(seperated(0) -> seperated(1))
    }

} 

请注意,只允许使用不可变地图 但我确信这是错的。请帮忙

感谢您的时间

1 个答案:

答案 0 :(得分:3)

使用.toMap和模式匹配等内置函数可以更好地完成这项工作:

def process_pops(lines: List[String]) : Map[String, Long] = {

  // Remove the first line that is the headers
  val withoutHeaders = lines.tail

  // Map over each line
  val tuples: List[(String, Long)] = withoutHeaders.map(line => {

    // Split the line on the commas into its two parts
    val Array(country, pop) = line.split(",")

    // Return a tuple of type Tuple2[String, Long]
    country -> pop.toLong
  })

  // Convert the List of Tuples to a Map
  tuples.toMap
}

您也可以使用fold执行此操作,我们将从空地图开始,并为每行添加一个新条目到地图:

def process_pops(lines: List[String]) : Map[String, Long] = {
  lines.tail.foldLeft(Map.empty[String, Long]) {
    case (map, line) =>
      val Array(country, pop) = line.split(",")
      map + (country -> pop.toLong)
  }
}