简体   繁体   English

将字符串转换为Map(String,List([Int,Int]))

[英]Convert a String to Map(String, List([Int,Int]))

I am currently learning Scala and I am trying to read lines of text from a file and convert them into a map of type - Map[String, List[(Int,Int)]] , but having difficulty getting there. 我目前正在学习Scala,并且尝试从文件中读取文本行并将其转换为Map[String, List[(Int,Int)]]类型的Map[String, List[(Int,Int)]] ,但是难以到达那里。

Example of text from file = London, -1:19, -2:21, 0:18, -3:24 文件中的文本示例= London, -1:19, -2:21, 0:18, -3:24

I want it to be returned as - Map(London -> List((-1,19), (-2,21), (0,18), (-3,24)) 我希望它以Map(London -> List((-1,19), (-2,21), (0,18), (-3,24))

I am able to get the city name by doing: 我可以通过以下操作获得城市名称:

for (line <- Source.fromFile(filename).getLines()) {
        val splitLine = line.split(",").map(_.trim)
        val city = splitLine.head

But I'm not able to get much further after this, I can't split the rest of the values into a list of ints. 但是在此之后,我无法走得更远,我无法将其余的值拆分为一个整数列表。

EDIT: 编辑:

Adding the full method in for clarification 添加完整方法以进行澄清

def readFile(filename: String): Map[String, List[(Int, Int)]] = {
    val result = for (line <- Source.fromFile(filename).getLines(); array = line.split(",").map(_.trim)) yield Map(array.head -> array.tail.map(x => {
      val y = x.split(":"); (y(0).toInt, y(1).toInt)
    }).toList)
    result
  }

You can do that in one line as 可以一行完成

val result = for (line <- Source.fromFile(filename).getLines(); array = line.split(",").map(_.trim)) yield Map(array.head -> array.tail.map(x => {val y = x.split(":"); (y(0).toInt, y(1).toInt)}).toList)

result should be result应该是

Map(London -> List((-1,19), (-2,21), (0,18), (-3,24)))
//Iterator[Map[String, (Int, Int}]]

result is an Iterator so you should change the return type accordingly result是一个Iterator因此您应该相应地更改返回类型

def readFile(filename: String): Iterator[Map[String, List[(Int, Int)]]] = {
    val result = for (line <- Source.fromFile(filename).getLines(); array = line.split(",").map(_.trim)) yield Map(array.head -> array.tail.map(x => {
      val y = x.split(":"); (y(0).toInt, y(1).toInt)
    }).toList)
    result
  }

Iterators are not really a collection and are not safe so I would suggest you to return list instead as 迭代器并不是真正的集合,也不安全,所以我建议您返回list而不是

def readFile(filename: String): List[Map[String, List[(Int, Int)]]] = {
  val result = for (line <- Source.fromFile(filename).getLines(); array = line.split(",").map(_.trim)) yield Map(array.head -> array.tail.map(x => {
    val y = x.split(":"); (y(0).toInt, y(1).toInt)
  }).toList)
  result.toList
}

Your comments suggested that you want 您的评论表明您想要

Not really how I would like it displayed as other cities added to the file will give me an output like: List(Map(London -> List((-1,19), (-2,21), (0,18), (-3,24))), Map(Edinburgh -> List((-3,16), (-2, 19), (-1, 21))) etc... instead of Map(London -> List((-1,19), (-2,21), (0,18), (-3,24))), Edinburgh -> List((-3,16), (-2, 19), (-1, 21))) almost like a key/value pair 并不是我希望将其显示为添加到文件中的其他城市的方式会给我这样的输出: List(Map(London -> List((-1,19), (-2,21), (0,18), (-3,24))), Map(Edinburgh -> List((-3,16), (-2, 19), (-1, 21)))等...而不是Map(London -> List((-1,19), (-2,21), (0,18), (-3,24))), Edinburgh -> List((-3,16), (-2, 19), (-1, 21)))几乎就像一个键/值对

For that all you need is .flatter.toMap and change the return type to Map[String, List[(Int, Int)]] 为此,您需要做的是.flatter.toMap并将返回类型更改为Map[String, List[(Int, Int)]]

def readFile(filename: String): Map[String, List[(Int, Int)]] = {
  val result = for (line <- Source.fromFile(filename).getLines(); array = line.split(",").map(_.trim)) yield Map(array.head -> array.tail.map(x => {
    val y = x.split(":"); (y(0).toInt, y(1).toInt)
  }).toList)
  result.toList.flatten.toMap
}

You can do as follows to get a Map[String,List[(Int, Int)]]] as you requested 您可以执行以下操作以根据需要获取Map[String,List[(Int, Int)]]]

for {
 line <- Source.fromFile(filename).getLines()
 val array = line.split(",")
}yield Map(array.head -> array.tail.toList.map(_.split(":").map(_.trim.toInt)).map(elem => (elem(0), elem(1))))

The biggest logic is in the tail management, to turn it to a tuple of Ints 最大的逻辑是在尾巴管理中,将其转换为Ints元组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM