[英]How can i get top 2 max values from an array of float numbers in swift?
我需要從浮點數數組中獲取 2 個最大值,即最高和第二高。是否有任何簡單的方法來獲取它們的索引,或者我是否需要為此更改數組的結構?
只需對數組進行排序並獲取所需的值
var array1 = [2.1, 2.2, 2.5, 3.0, 4.2, 2]
var array2 = array1.sort(){ $0 > $1}
//One way
let firstMax = array2[0]
let secondMax = array2[1]
//Second way
let firstMax = array2.removeFirst()
let secondMax = array2.removeFirst()
編輯
如果你想要索引只是讓它們像
let maxPos = array1.indexOf(firstMax)
let secMaxPos = array1.indexOf(secondMax)
如果您對這些東西感到困惑,請遵循以下正常基礎知識。
var max = -1.0, maxPos = -1, secMax = -1.0, secMaxPos = -1
for (index, value) in array1.enumerate() {
if value > max {
max = value
maxPos = index
} else if value > secMax {
secMax = value
secMaxPos = index
}
}
print("max:\(max)->pos:\(maxPos)::secMax:\(secMax)->secMaxPos:\(secMaxPos)")
您可以使用enumerate()
創建一個包含 (index, value) 的元組數組,然后按值對該數組進行排序以找到兩個最大值:
let arr: [Float] = [1.2, 3.14, 1.609, 2.718, 0.3]
// Create an array of (index, value) tuples sorted by value
// in decreasing order
let result = arr.enumerate().sort { $0.1 > $1.1 }
print(result)
[(1, 3.1400001), (3, 2.71799994), (2, 1.60899997), (0, 1.20000005), (4, 0.300000012)]
let (topIndex, top) = result[0]
print("top = \(top), index = \(topIndex)")
top = 3.14, index = 1
let (secondIndex, second) = result[1]
print("second = \(second), index = \(secondIndex)")
second = 2.718, index = 3
import Algorithms
[1, 3, 5, 6, 4, 2].max(count: 2) // [5, 6]
https://github.com/apple/swift-algorithms/blob/main/Guides/MinMax.md
試試你的游戲
var item = [2.01 ,3.95,1.85,2.65,1.6]
var sorted = item.sort({ $0 > $1 })
打印(排序)
基本思想是遍歷數組並挑選出最大和第二大的項目。
一些可能難以閱讀的短代碼:
let myFloatArray : [Float] = ...
let top2 = myFloatArray.enumerated().reduce(((-1, Float.nan), (-1, Float.nan)), combine: { t, v in
// Check if value is larger than first item in tuple
if (!(v.1 <= t.0.1)) {
// Check if value is larger than second item in tuple
if (!(v.1 <= t.1.1)) {
// Return with new value as largest
return (t.1, v)
} else {
// Return with new value as next largest
return (v, t.1)
}
}
// Return old result
return t
})
或者使用更明確的變量名稱:
var largestIndex = -1;
var largestValue = Float.nan;
var secondLargestIndex = -1;
var secondLargestValue = Float.nan;
for index in 0..<myFloatArray.count {
let value = myFloatArray[index];
if (!(value <= secondLargestValue)) {
if (!(value <= largestValue)) {
secondLargestValue = largestValue;
secondLargestIndex = largestIndex;
largestValue = value;
largestIndex = index;
} else {
secondLargestValue = value;
secondLargestIndex = index;
}
}
}
var array1 = [1,2,3,4,5]
var array2 = array1.sorted(){ $0 > $1}
打印(數組2)
//一種方式
讓 firstMax = array2[0]
讓 secondMax = array2[1]
//回答
打印(firstMax)
打印(第二個最大)
func secondLargest(arr: [Int]){
var max1 = -1;
var max2 = -1;
for item in arr {
if item > max1 {
max2 = max1;
max1 = item
}else if item > max2 {
max2 = item
}
}
}
import UIKit var array = [6,4,3,5,9,7,8,2,10] var maxValue = 0, temp = 0, maxIndex = 0 for (index, item) in array.enumerated() { if (item > temp){ maxIndex = index } } array.remove(at: maxIndex) for (_, item) in array.enumerated() { if (item > maxValue){ maxValue = item } } print("---------") print(maxValue) print("---------")
var numberArray = [4.4,5.3,3.2,2.1,6.0,1.2,9.6,8.0,9.4]
var secontLargestValue = 0.0
var firstLargestValue = 0.0
for value in numberArray {
if value > firstLargestValue {
secontLargestValue = firstLargestValue
firstLargestValue = value
} else if (value > secontLargestValue && value != firstLargestValue)
{
secontLargestValue = value
}
}
print("First largest value - \(firstLargestValue)")
print("Second largest value - \(secontLargestValue)")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.