简体   繁体   English

比较Double Swift 4的两个数组

[英]Compare two arrays of Double Swift 4

Have two arrays of Doubles, I want to compare them and create a new array with result of the difference, but not sure how to create a loop for that. 有两个Doubles数组,我想将它们进行比较并创建一个具有差异结果的新数组,但不确定如何为此创建循环。 Please advise. 请指教。

Example of the arrays here: 数组示例:

 var freshRates = [1.6,1.7,2.0]
 var oldRates = [1.5,1.4,1.9]
 var difference: [Double] = []

Zip the arrays to get an array of tuples, and then just use map to calculate difference for each pair: 压缩数组以获得元组数组,然后仅使用map计算每对的差:

var freshRates = [1.6,1.7,2.0]
var oldRates = [1.5,1.4,1.9]
var difference: [Double] = zip(freshRates, oldRates).map({ $0.0 - $0.1 })
var freshRates = [1.6,1.7,2.0]
var oldRates = [1.5,1.4,1.9]
var difference: [Double] = []

for (val1,val2) in zip(freshRates, oldRates){
    difference.append(val2 - val1)
}

debugPrint(difference)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM