如何更快速地转置数组? [英] How to transpose an array more Swiftly?

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

问题描述

不久前,我问了类似的问题.有人问我怎样才能变成这样的数组:

I asked a similar question a while ago. It was asking how can I turn an array like this:

[[1,2,3],[4,5,6],[7,8,9]]

对此:

[1,2,3,4,5,6,7,8,9]

但是现在我想把相同的数组变成这个:

But now I want to turn that same array to this:

[1,4,7,2,5,8,3,6,9]

假设所有子数组的长度相同.

Assume all the subarrays have the same length.

如果您尚未注意到,结果中的前三项就是这三个子数组中的第一项.结果中的第四,第五和第六项是每个子数组的第二项.

If you haven't noticed already, the first three items in the result is the first item of the three subarrays. The fourth, fifth and sixth items in the result is the second item of each subarray.

如果您仍然不了解,也许会有所帮助:

If you still don't understand, maybe this will help:

原始数组:

[
    [1,2,3],
    [4,5,6],
    [7,8,9]
]

结果:

[
    1,4,7,
    2,5,8,
    3,6,9
]

此刻,我有这个:

func flatten(array: [[Int]]) -> [Int] {
    var flat = [Int]()
    for i in 0..<array[0].count {
        for subarray in array {
            flat.append(subarray[i])
        }
    }
    return flat
}

我不认为这很花哨.我怎么能迅速做到这一点?

I don't think that is very swfity. How can I do this in a swifty way?

为了避免成为XY问题,这就是为什么我要这样做.

Just to avoid being an XY problem, here's why I want to do this.

我正在开发一个棋盘游戏.我正在从 HLSpriteKit 使用HLGridNode(基本上是一堆正方形的网格状布局)作为棋盘游戏的棋盘.要编辑网格节点的内容,我需要传递一个Sprite节点的1D数组,而不是2D数组.

I am developing a board game. I am using HLGridNode (It's basically a bunch of squares in a grid-like layout) from HLSpriteKit as the board game's board. To edit the contents of the grid node, I need to pass in an 1D array of sprite nodes, not a 2D array.

为了使生活更轻松,我将模型对象存储在2D数组中.这样,通过执行以下操作,我可以从左边引用正方形5个正方形,从上面引用2个正方形.

To make my life easier, I stored the model objects in a 2D array. This way, I can refer to the sqaure 5 squares from the left and 2 squares from the top just by doing:

modelObjects[5][2]

如果我使用.flatMap { $0 }展平2D数组并将结果传递到网格节点,则modelObjects[5][2]似乎从左起2个正方形,从顶部起5个正方形.

If I flatten the 2D array using .flatMap { $0 } and pass the result to the grid node, modelObjects[5][2] would appear to be 2 squares from the left and 5 squares from the top.

这不是的重复项,因为该问题似乎具有一定数量的数组.尽管我可以将2D数组放入一个循环中,并执行这些enumerate().map {...}任务,但这似乎是一个漫长的过程.我认为使用2D阵列必须做得更简单.

This is not a duplicate of this because that question seems to have definite number of arrays to work with. Although I can put my 2D array into a loop, and do those enumerate().map {...} stuff, it seems like a really long-winded approach. I think there must be a simpler to do this with 2D arrays.

推荐答案

这是对 Shadow Of 的改进s answer :

extension Collection where Self.Iterator.Element: RandomAccessCollection { 
    func transposed() -> [[Self.Iterator.Element.Iterator.Element]] {
        guard let firstRow = self.first else { return [] }
        return firstRow.indices.map { index in
            self.map{ $0[index] }
        }
    }
}

let matrix = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
]
matrix.transposed().forEach{ print($0) }

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

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