如何交错两个数组?

时间:2022-09-06 15:35:43

If I have two arrays e.g

如果我有两个数组,例如

let one = [1,3,5]
let two = [2,4,6]

I would like to merge/interleave the arrays in the following pattern [one[0], two[0], one[1], two[1] etc....]

我想以下列模式合并/交错数组[one [0],two [0],one [1],two [1] etc ....]

//prints [1,2,3,4,5,6]
let comibned = mergeFunction(one, two)
print(combined)

What would be a good way to implement the combining function?

实现组合功能的好方法是什么?

func mergeFunction(one: [T], _ two: [T]) -> [T] {
    var mergedArray = [T]()
    //What goes here
    return mergedArray
}

2 个解决方案

#1


20  

If both arrays have the same length then this is a possible solution:

如果两个阵列具有相同的长度,那么这是一个可能的解决方案:

let one = [1,3,5]
let two = [2,4,6]

let merged = zip(one, two).flatMap { [$0, $1] }

print(merged) // [1, 2, 3, 4, 5, 6]

Here zip() enumerates the arrays in parallel and returns a sequence of pairs (2-element tuples) with one element from each array. flatMap() creates a 2-element array from each pair and concatenates the result.

这里zip()并行枚举数组,并返回一对对(2元素元组)的序列,每个数组都有一个元素。 flatMap()从每对创建一个2元素数组并连接结果。

If the arrays can have different length then you append the extra elements of the longer array to the result:

如果数组的长度不同,则将较长数组的额外元素追加到结果中:

func mergeFunction<T>(one: [T], _ two: [T]) -> [T] {
    let commonLength = min(one.count, two.count)
    return zip(one, two).flatMap { [$0, $1] } 
           + one.suffixFrom(commonLength)
           + two.suffixFrom(commonLength)
}

Update for Swift 3:

Swift 3的更新:

func mergeFunction<T>(_ one: [T], _ two: [T]) -> [T] {
    let commonLength = min(one.count, two.count)
    return zip(one, two).flatMap { [$0, $1] } 
           + one.suffix(from: commonLength)
           + two.suffix(from: commonLength)
}

#2


5  

If you're just looking to interleave two arrays, you could just do something like:

如果您只是想要交错两个数组,您可以执行以下操作:

let maxIndex = max(one.count, two.count)
var mergedArray = Array<T>()
for index in 0..<maxIndex {
    if index < one.count { mergedArray.append(one[index]) }
    if index < two.count { mergedArray.append(two[index]) }
}

return mergedArray

#1


20  

If both arrays have the same length then this is a possible solution:

如果两个阵列具有相同的长度,那么这是一个可能的解决方案:

let one = [1,3,5]
let two = [2,4,6]

let merged = zip(one, two).flatMap { [$0, $1] }

print(merged) // [1, 2, 3, 4, 5, 6]

Here zip() enumerates the arrays in parallel and returns a sequence of pairs (2-element tuples) with one element from each array. flatMap() creates a 2-element array from each pair and concatenates the result.

这里zip()并行枚举数组,并返回一对对(2元素元组)的序列,每个数组都有一个元素。 flatMap()从每对创建一个2元素数组并连接结果。

If the arrays can have different length then you append the extra elements of the longer array to the result:

如果数组的长度不同,则将较长数组的额外元素追加到结果中:

func mergeFunction<T>(one: [T], _ two: [T]) -> [T] {
    let commonLength = min(one.count, two.count)
    return zip(one, two).flatMap { [$0, $1] } 
           + one.suffixFrom(commonLength)
           + two.suffixFrom(commonLength)
}

Update for Swift 3:

Swift 3的更新:

func mergeFunction<T>(_ one: [T], _ two: [T]) -> [T] {
    let commonLength = min(one.count, two.count)
    return zip(one, two).flatMap { [$0, $1] } 
           + one.suffix(from: commonLength)
           + two.suffix(from: commonLength)
}

#2


5  

If you're just looking to interleave two arrays, you could just do something like:

如果您只是想要交错两个数组,您可以执行以下操作:

let maxIndex = max(one.count, two.count)
var mergedArray = Array<T>()
for index in 0..<maxIndex {
    if index < one.count { mergedArray.append(one[index]) }
    if index < two.count { mergedArray.append(two[index]) }
}

return mergedArray