xcode 在 Swift 中使用“Map”创建两个数组的超集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37532205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:50:54  来源:igfitidea点击:

Using "Map" in Swift To Create Superset of Two Arrays

iosarraysswiftxcode

提问by doc92606

Let's say I have two arrays:

假设我有两个数组:

let letterArray = ["a", "b", "c", "d", "e"...]
let numberArray = [1, 2, 3, 4, 5, 6, 7...]

I want to combine the two arrays so that I would get an output of

我想组合这两个数组,以便获得输出

["a1", "b2", "c3", "d4", "e5"]

How would I go about doing that?

我该怎么做呢?

回答by Alexander - Reinstate Monica

You can use zip(_:_:)before map:

您可以zip(_:_:)在地图之前使用:

let a = ["a", "b", "c", "d", "e"]
let b = [1, 2, 3, 4, 5]

let result = zip(a, b).map { 
let result = letterArray.enumerate().map { 
let result = letterArray.enumerate().flatMap {
    guard numberArray.count > 
func zip<Sequence1, Sequence2>(_ sequence1: Sequence1, _ sequence2: Sequence2) -> Zip2Sequence<Sequence1, Sequence2> where Sequence1 : Sequence, Sequence2 : Sequence
.index else { return .None } return
let letterArray = ["a", "b", "c", "d", "e"]
let numberArray = [1, 2, 3, 4, 5, 6, 7]

let zipSequence = zip(letterArray, numberArray)
let finalArray = zipSequence.map({ (tuple: (letter: String, number: Int)) -> String in
    return tuple.letter + String(tuple.number)
})

print(finalArray) // prints ["a1", "b2", "c3", "d4", "e5"]
.element + String(numberArray[
let letterArray = ["a", "b", "c", "d", "e"]
let numberArray = [1, 2, 3, 4, 5, 6, 7]

let finalArray = zip(letterArray, numberArray).map { 
let letterArray = ["a", "b", "c", "d", "e"]
let numberArray = [1, 2, 3, 4, 5, 6, 7]

let zipSequence = zip(letterArray, numberArray)
let finalArray = zipSequence.reduce([]) { (partialResult: [String], tuple: (letter: String, number: Int)) -> [String] in
    return partialResult + [tuple.letter + String(tuple.number)]
}

print(finalArray) // prints ["a1", "b2", "c3", "d4", "e5"]
.0 + String(
extension Array where Element == String {

    func mergeLettersWithNumbers(from numberArray: [Int]) -> [String] {
        var index = startIndex
        let iterator: AnyIterator<String> = AnyIterator {
            defer { index = self.index(index, offsetBy: 1) }
            guard index < self.endIndex, index < numberArray.endIndex else { return nil }
            return self[index] + String(numberArray[index])
        }
        return Array(iterator)
    }

}

let letterArray = ["a", "b", "c", "d", "e"]
let numberArray = [1, 2, 3, 4, 5, 6, 7]

let newArray = letterArray.mergeLettersWithNumbers(from: numberArray)
print(newArray) // prints ["a1", "b2", "c3", "d4", "e5"]
.1) } print(finalArray) // prints ["a1", "b2", "c3", "d4", "e5"]
.index]) } as [String]
.element + String(numberArray[##代码##.index]) }
+ String() } print(result) // => ["a1", "b2", "c3", "d4", "e5"]

You can try this code here.

您可以在此处尝试此代码。

zip(_:_:)produces a custom Zip2Sequence, which has a special implmentation of the SequenceTypeprotocol, so that it iterates pairs made from the two source collections.

zip(_:_:)生成一个 custom Zip2Sequence,它具有SequenceType协议的特殊实现,以便它迭代由两个源集合组成的对。

回答by fpg1503

Actually you cando it using only map!

实际上你可以只使用map!

If the two sequences have the same size just enumerateand map:

如果两个序列的大小相同,则enumeratemap

##代码##

If you're not sure which one is larger and you wanna trim using the smaller just flatMapthe undesired values away:

如果您不确定哪个更大并且您想使用较小flatMap的值来修剪不需要的值:

##代码##

回答by Imanou Petit

#1. Using zip(_:_:)to combine elements of an array of Stringwith elements of an array of Intinto a new array of String

#1. 使用zip(_:_:)到的阵列的元素结合String使用的阵列的元素Int到一个新的数组String

With Swift 3, Swift Standard Library provides zip(_:_:)function. zip(_:_:)has the following declaration:

在 Swift 3 中,Swift 标准库提供了zip(_:_:)功能。zip(_:_:)有以下声明:

##代码##

Creates a sequence of pairs built out of two underlying sequences.

创建由两个底层序列构建的对序列。



In order to get a new array from a Zip2Sequenceinstance, you can use Zip2Sequence's map(_:)method. The Playground code below that uses map(_:)combines your letter and number elements into a new array of String:

为了从Zip2Sequence实例中获取新数组,您可以使用Zip2Sequence'smap(_:)方法。下面使用的 Playground 代码map(_:)将您的字母和数字元素组合成一个新的 数组String

##代码##

You can refactor the previous code with a much concise style:

你可以用更简洁的风格重构之前的代码:

##代码##

As an alternative to map(_:), you can use Zip2Sequence's reduce(_:_:)method:

作为 的替代方法map(_:),您可以使用Zip2Sequencereduce(_:_:)方法:

##代码##

#2. Using an Arrayextension custom method to combine elements of an array of Stringwith elements of an array of Intinto a new array of String

#2. 使用Array扩展自定义方法将数组的String元素与数组的元素组合Int成一个新的数组String

If you don't want to use zip(_:_:), you can create your own Arrayextension method in order to have the expected result. The Playground code below shows how to create it:

如果您不想使用zip(_:_:),您可以创建自己的Array扩展方法以获得预期的结果。下面的 Playground 代码显示了如何创建它:

##代码##