Arrays 将多个数组合并为一个,按顺序索引

Arrays 将多个数组合并为一个,按顺序索引,arrays,swift,functional-programming,Arrays,Swift,Functional Programming,我有多个阵列(最多15个),每个阵列最多可以有1800个对象 我需要将它们组合成一个数组,这样我就可以应用分隔符(“,”)来生成csv文件。问题是,当我将它们组合到一个数组中时,它必须按顺序组合,就像每个数组的第一个对象应该首先插入,然后是对象的第二个索引,然后是第三个索引,依此类推 通过使用for-in循环,我能够实现我想要的结果。然而,这并不是很快。我觉得可以使用swift中可用的函数方法(使用map、reduce和filter函数)以更干净的方式完成 然而,我无法将它们完美地结合起来。有谁

我有多个阵列(最多15个),每个阵列最多可以有1800个对象

我需要将它们组合成一个数组,这样我就可以应用分隔符(“,”)来生成csv文件。问题是,当我将它们组合到一个数组中时,它必须按顺序组合,就像每个数组的第一个对象应该首先插入,然后是对象的第二个索引,然后是第三个索引,依此类推

通过使用for-in循环,我能够实现我想要的结果。然而,这并不是很快。我觉得可以使用swift中可用的函数方法(使用map、reduce和filter函数)以更干净的方式完成

然而,我无法将它们完美地结合起来。有谁能帮助我使用swift函数方法来实现结果


附言:如果您想让我发布for-in-loop代码,请告诉我,但我相信这不是必需的。

以下是我对您在文章中描述的问题的函数方法

首先,我使用枚举方法将数组展开,该方法返回一个元组,其中包含数组中的元素位置及其值

在这之后,就有了一个包含这个元组的数组,下一步,根据每个元素的偏移量(位置)值对这个大数组进行排序

数组排序后,您可以使用map函数提取值

最后一步,一旦我们有了一个带有排序值的数组,就必须使用reduce函数将其缩减为字符串

// A group of arrays
var array1: [Int] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
var array2: [Int] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
var array3: [Int] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
var array4: [Int] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

// This is **your** array
let bigOne = [ array1, array2, array3, array4 ]

// And here is the functional concatenation.
let flattedOne = bigOne.flatMap({ $0.enumerated() }).sorted(by: { $0.0 < $0.1 }).map({$0.element}).reduce("") 
{
    return $0.isEmpty ? "\($1)" : "\($0), \($1)"

}


print(flattedOne)
//一组数组
变量数组1:[Int]=[1,2,3,4,5,6,7,8,9,10]
变量数组2:[Int]=[1,2,3,4,5,6,7,8,9,10]
变量数组3:[Int]=[1,2,3,4,5,6,7,8,9,10]
变量数组4:[Int]=[1,2,3,4,5,6,7,8,9,10]
//这是**您的**数组
设bigOne=[array1,array2,array3,array4]
//这里是函数连接。
设flattedOne=bigOne.flatMap({$0.enumerated()}).sorted(by:{$0.0<$0.1}).map({$0.element}).reduce(“”)
{
返回$0.i空?”\($1):“\($0),\($1)”
}
打印(平板打印)
给定4个(或更多)数组

枚举每个数组并将它们放入另一个数组中

let lists = [
    list0.enumerate().map { (index: $0, array: 0, value: $1) },
    list1.enumerate().map { (index: $0, array: 1, value: $1) },
    list2.enumerate().map { (index: $0, array: 2, value: $1) },
    list3.enumerate().map { (index: $0, array: 3, value: $1) }
]
现在你可以写作了

let sorted = lists
    .flatten()
    .sort { ($0.index, $0.array) < ($1.index, $1.array) }
    .map { $0.value }
let sorted=列表
.flatte()
.sort{($0.index,$0.array)<($1.index,$1.array)}
.map{$0.value}
[1,10,100,1000,2,20,200,2000,3,30,300,3000,6,40,400,4000,7,50,500,5000,8,60,600,6000,9,70,700,7000,80,800,8000,90,900,9000]


我会考虑将这扩展为数组的数组(虽然注意到你不能直接这样做,请参阅)。然后,您可以结合使用
reduce(::)
flatMap(:)
两种风格,通过迭代内部集合的长度并提取每个给定索引处的元素,顺序合并数组

extension Array where Element : RandomAccessCollection, Element.Index == Int, Element.IndexDistance == Element.Index {

    func joinedByTransposing() -> [Element.Iterator.Element] {

        // The maximum length of the inner collections. Obviously if the 2D array is
        // guaranteed to be n*m, you can optimise by just taking the first inner
        // collection's count (and obviously you'll want to check that the array isn't empty first).
        let maxIndex = self.reduce(0, {$0 > $1.count ? $0 : $1.count})

        // Iterates through the max length of the inner collections, joining the restantant collections
        // from the transform below into a single array.
        return (0..<maxIndex).flatMap { index in

            // Iterate through each inner collection, getting the element at the current index of iteration,
            // or returning nil if the index is out of bounds. This flatMap will filter out any nils.
            // If the 2D array is guarenteed to be n*m, this can be replaced by self.map { $0[index] }
            self.flatMap { innerArray in

                // Simple bounds check to get the element at the given index, or nil if out of bounds
                index < innerArray.count ? innerArray[index] : nil
            }
        }
    }
}

值得注意的是,此解决方案的总体时间复杂度为O(n*m)–而使用
排序(按:)
的解决方案的时间复杂度至少为O(n*m*log(n*m))。对于大型阵列,这一额外成本可能非常重要。

这里有另一种方法

public func sort(compound array: [[Int]]) -> [Int]
{
    let max_index: Int = array.map({ $0.count }).max()!

    var sorted: [Int] = [Int]()


    (0 ..< max_index).forEach({ index in
        array.forEach()
        { 
            if $0.count > index
            {
                sorted.append($0[index])
            }
        }
    })

   return sorted
}


// A group of arrays
var array1: [Int] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 111, 112 ]
var array2: [Int] = [ 10, 20, 3, 4, 5, 6, 7, 8, 9, 10 ]
var array3: [Int] = [ 1000, 2000, 3, 4, 5, 600, 7, 8, 9, 10, 11 ]
var array4: [Int] = [ 100, 200, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]

let bigOne: [[Int]] = [ array1, array2, array3, array4 ]

let sorted: [Int] = sort(compound: bigOne)
print(sorted)

我试着用这个做实验,它在排序函数之后给出了错误的输出。我将相同的值更改为包含不同的值。例如:第一个数组包含1,2,3;第二个数组包含10,20,30;第三个100,200,300,。。第四个1000,2000,3000。。。等排序后的函数,它来像,1,10,1000,100,其中,因为它应该是1,10,100,1000。这里点错菜了。嘿,你能和我们一起在这里聊天吗。我也尝试了不同的值和不同大小的数组。按计划工作。非常感谢你。将您的最后一行更新为swift 3.0语法。它工作得很好,但是仔细查看后我仍然有一个疑问,排序函数的条件是如何工作的?{$0.0<$0.1},您正在比较同一元组的偏移量和元素,而不是比较下一个元素的偏移量。无法消化。:)@拉姆斯瓦尔:事实上你是对的<代码>$0.0<$0.1应该是
$0.0<$1.0
。非常感谢。刚刚更新了我的代码。等等,但这并没有给出正确的答案。我已经测试过了。顺序从1、10、100、1000变为1、10、1000、100。@Rameswar:
$0.0<$1.0
告诉我:[1、10、100、2、20、200、3、30、300]是的,看到了。我也在尝试不同的解决方案,因为这似乎是个好问题。如果我在周末找到更好的答案,我会更新我的答案,否则我会接受下面给出的答案之一。干杯!!:)
let array0 = [1,   2,   3,   4   ]
let array1 = [10,  20,  30       ]
let array2 = [100, 200, 300, 6, 7]

let result = [array0, array1, array2].joinedByTransposing()

print(result)

// [1, 10, 100, 2, 20, 200, 3, 30, 300, 4, 6, 7]
public func sort(compound array: [[Int]]) -> [Int]
{
    let max_index: Int = array.map({ $0.count }).max()!

    var sorted: [Int] = [Int]()


    (0 ..< max_index).forEach({ index in
        array.forEach()
        { 
            if $0.count > index
            {
                sorted.append($0[index])
            }
        }
    })

   return sorted
}


// A group of arrays
var array1: [Int] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 111, 112 ]
var array2: [Int] = [ 10, 20, 3, 4, 5, 6, 7, 8, 9, 10 ]
var array3: [Int] = [ 1000, 2000, 3, 4, 5, 600, 7, 8, 9, 10, 11 ]
var array4: [Int] = [ 100, 200, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]

let bigOne: [[Int]] = [ array1, array2, array3, array4 ]

let sorted: [Int] = sort(compound: bigOne)
print(sorted)
print(sorted.reduce("") { return $0.isEmpty ? "\($1)" : "\($0), \($1)" })