如何在Swift 4中将金字塔的String类型转换为双精度数组 [英] How to convert pyramid type of String to array of doubles in Swift 4

查看:109
本文介绍了如何在Swift 4中将金字塔的String类型转换为双精度数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通读下面给出的字符串,以便从此字符串中将双打数组分隔成几行,因此每行将是一个不同的双精度数组.

I need to read through the string given below, so that I will have arrays of doubles from this string seperated by lines, so each line will be a different array of doubles.

var sampleString = """
                     55
                    94 48
                   95 30 96
                 77 71 26 67
                97 13 76 38 45
              07 36 79 16 37 68
             48 07 09 18 70 26 06
           18 72 79 46 59 79 29 90
          20 76 87 11 32 07 07 49 18
        27 83 58 35 71 11 25 57 29 85
       14 64 36 96 27 11 58 56 92 18 55
     02 90 03 60 48 49 41 46 33 36 47 23
    92 50 48 02 36 59 42 79 72 20 82 77 42
  56 78 38 80 39 75 02 71 66 66 01 03 55 72
 44 25 67 84 71 67 11 61 40 57 58 89 40 56 36
85 32 25 85 57 48 84 35 47 62 17 01 01 99 89 52
06 71 28 75 94 48 37 10 23 51 06 48 53 18 74 98 15
27 02 92 23 08 71 76 84 15 52 92 63 81 10 44 10 69 93
"""

例如:第一个数组= [55],第二个数组= [94,48],依此类推.

For example: 1st array = [55], 2nd array = [94, 48] and so on.

我知道也有类似的问题,我尝试了以下字符串方法:replaceOccurances(of:with :),trimmingCharacters(in :),split(seperator :)等,然后我首先尝试了类似的方法:

I know there are similar questions to this and I tried string methods like: replacingOccurances(of:with:), trimmingCharacters(in:), split(seperator:) and so on, and I tried something like this first:

let lineSeperatedString = sampleString.replacingOccurrences(of: "\n", with: ",")

    let whiteSpaceTrimmedString = lineSeperatedString.replacingOccurrences(of: "  ", with: "")

    print(whiteSpaceTrimmedString)

然后输出就像:

 55,94 48, 95 30 96, 77 71 26 67,97 13 76 38 45,07 36 79 16 37 68, 48 07 09 18 70 26 06, 18 72 79 46 59 79 29 90,20 76 87 11 32 07 07 49 18,27 83 58 35 71 11 25 57 29 85, 14 64 36 96 27 11 58 56 92 18 55, 02 90 03 60 48 49 41 46 33 36 47 23,92 50 48 02 36 59 42 79 72 20 82 77 42,56 78 38 80 39 75 02 71 66 66 01 03 55 72, 44 25 67 84 71 67 11 61 40 57 58 89 40 56 36, 85 32 25 85 57 48 84 35 47 62 17 01 01 99 89 52,06 71 28 75 94 48 37 10 23 51 06 48 53 18 74 98 15,27 02 92 23 08 71 76 84 15 52 92 63 81 10 44 10 69 93

足够接近:(如果您忽略每个部分开头用逗号分隔的空白

Close enough :( if you ignore some whitespaces in the beginning of each part separated with a coma

然后我尝试了如下操作,以便可以将它们转换为Ints,但是随后失败了,并将每个数字都转换为int本身;

Then I tried something like following so that I can convert them to Ints but then it failed and converted each number as an int itself;

let myUpdatedArray = whiteSpaceTrimmedString.split(separator: "\n")
    for i in myUpdatedArray {
        let temp = i.map{String($0)}
        for j in temp {
            if let myInt = Int(j) {
                print(myInt)
            }
        }

    }

抱歉,它太长了,但是我想展示我尝试过的所有内容,但不能自己弄清楚

Sorry its a bit too long but I wanted to show everything I tried, but could not figure it out on my own

推荐答案

您可以使用

  • components(separatedBy:.newlines)将字符串分成行,
  • map 将线映射到(外部)数组
  • components(separatedBy:.whitespaces)将每一行分成单独的数字
  • compactMap(Double.init)将一行中的数字转换为 Double 的(内部)数组(忽略无效的浮点数).
  • components(separatedBy: .newlines) to separate the string into lines,
  • map to map the lines to the (outer) array,
  • components(separatedBy: .whitespaces) to separate each line into separate numbers,
  • compactMap(Double.init) to convert the numbers in a line to the (inner) array of Double (ignoring invalid floating point numbers).

放在一起:

import Foundation

var sampleString = """
55
94 48
95 30 96
"""

let values = sampleString.components(separatedBy: .newlines).map {
    $0.components(separatedBy: .whitespaces).compactMap(Double.init)
}

print(values) // [[55.0], [94.0, 48.0], [95.0, 30.0, 96.0]]

这篇关于如何在Swift 4中将金字塔的String类型转换为双精度数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆