通过对其他两个数组的所有元素应用二进制运算,在Swift中创建一个数组 [英] Creating an array in Swift by applying binary operation to all elements of two other arrays
本文介绍了通过对其他两个数组的所有元素应用二进制运算,在Swift中创建一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Swift中是否存在通过在其他两个数组的元素上应用二进制运算来创建数组的简洁方法?
Is there a concise way in Swift of creating an array by applying a binary operation on the elements of two other arrays?
例如:
let a = [1, 2, 3]
let b = [4, 5, 6]
let c = (0..<3).map{a[$0]+b[$0]} // c = [5, 7, 9]
推荐答案
如果使用 zip 到组合元素,您可以仅使用+
来引用+
:
If you use zip to combine the elements, you can refer to +
with just +
:
let a = [1, 2, 3]
let b = [4, 5, 6]
let c = zip(a, b).map(+) // [5, 7, 9]
这篇关于通过对其他两个数组的所有元素应用二进制运算,在Swift中创建一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文