如何转置字符串数组 [英] How to transpose an array of strings

查看:31
本文介绍了如何转置字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 txt,其中包含以下格式的一些数据.

I've a txt including some data in the following format.

AYGA:GKA:GOROKA:GOROKA:PAPUA NEW GUINEA:06:04:54:S:145:23:30:E:5282
AYLA:LAE::LAE:PAPUA NEW GUINEA:00:00:00:U:00:00:00:U:0000
AYMD:MAG:MADANG:MADANG:PAPUA NEW GUINEA:05:12:25:S:145:47:19:E:0020

如何分隔以冒号(:")区分的每个项目以及如何将每个部分加载到如下示例中的数组中?

How to separate each item distinguished with colons(":") and how to load each section to an array like in the example below?

var array1 = ["AYGA", "AYLA", "AYMD"]
var array2 = ["GKA", "LAE", "MAG"]
var array3 = ["GOROKA", "", "MADANG"]
var array4 = ["GOROKA", "LAE", "MADANG"]
var array5 = ["PAPUA NEW GUINEA", "PAPUA NEW GUINEA", "PAPUA NEW GUINEA"]
var array6 = ["06", "00", "05"]
var array7 = ["04", "00", "12"]
var array8 = ["54", "00", "25"]
var array9 = ["S", "U", "S"]
var array10 = ["145", "00", "145"]
var array11 = ["23", "00", "47"]
var array12 = ["30", "00", "19"]
var array13 = ["E", "U", "E"]
var array14 = ["5282", "0000", "0020"]

推荐答案

您尝试执行的操作称为换位.转动一个看起来像这样的数组:

What you are trying to do is called a transposition. Turning an array that looks like:

[[1, 2, 3], [4, 5, 6]]

变成一个看起来像这样的数组:

into an array that looks like:

[[1, 4], [2, 5], [3, 6]]

为此,让我们为转置定义一个通用函数并将其应用于您的问题

To do this, let's define a generic function for transposition and apply it to your problem

// Import the text file from the bundle

guard
    let inputURL = NSBundle.mainBundle().URLForResource("input", withExtension: "txt"),
    let input = try? String(contentsOfURL: inputURL)
    else { fatalError("Unable to get data") }

// Convert the input string into [[String]]
let strings = input.componentsSeparatedByString("\n").map { (string) -> [String] in
    string.componentsSeparatedByString(":")
}

// Define a generic transpose function.
// This is the key to the solution.

public func transpose<T>(input: [[T]]) -> [[T]] {
    if input.isEmpty { return [[T]]() }
    let count = input[0].count
    var out = [[T]](count: count, repeatedValue: [T]())
    for outer in input {
        for (index, inner) in outer.enumerate() {
            out[index].append(inner)
        }
    }

    return out
}

// Transpose the strings
let results = transpose(strings)

可以用

for result in results {
    print("\(result)")
}

生成(例如)

["AYGA", "AYLA", "AYMD"]
["GKA", "LAE", "MAG"]
["GOROKA", "", "MADANG"]
["GOROKA", "LAE", "MADANG"]
["PAPUA NEW GUINEA", "PAPUA NEW GUINEA", "PAPUA NEW GUINEA"]
["06", "00", "05"]
["04", "00", "12"]
["54", "00", "25"]
["S", "U", "S"]
["145", "00", "145"]
["23", "00", "47"]
["30", "00", "19"]
["E", "U", "E"]
["5282", "0000", "0020"]

这样做的优点是不依赖于您拥有的数组数量,子数组的数量取自第一个数组的计数.

This has the advantage of not depending on the number of arrays that you have, and the number of subarrays is taken from the count of the first array.

您可以为此下载示例游乐场,其中包含作为 Playground 资源中的文件输入.

You can download an example playground for this, which has the input as a file in the playground's resources.

这篇关于如何转置字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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