我需要从float数组中获得2个最大值,即一个最高和一个最高的值。是否有任何简单的方法来获取它们的索引,或者是否需要将数组更改为此结构?如何从swift中的float数组中获取前2个最大值?
1
A
回答
3
只是排序的数组,如果你想索引只是拿它们喜欢采取必要的值
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)")
+0
你的代码是好的,但它不提供索引。 –
+0
是的,我已经更新了答案,检查出来。 –
2
可以使用enumerate()
按值创建包含元组(指数值)的数组,然后排序该数组找到最大的两个值:
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
0
尝试在你的游戏gorund
VAR项目= 2.01,3.95,1.85,2.65,1.6]
VAR分类= item.sort({$ 0> $ 1})
打印(排序)
0
基本的想法是遍历数组,并挑出最大和次大的项目。
一些短码,可能是难以阅读:
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;
}
}
}
相关问题
- 1. 如何在Swift中获取元组数组的最大Double值?
- 2. 如何从2个组件的UIPickerView中获取值? swift
- 3. 如何从OpenCV中的n维数组中获取最大值
- 4. 如何在swift中获取数组中最大元素的个数
- 5. 如何获取MongoDB中的子数组的最小/最大值?
- 6. 从JavaScript数组中获取最大值和最小值
- 7. 如何从数组中提取最大值和最小值?
- 8. 如何获取数组列表中的最小/最大值?
- 9. 如何获取单个表中每组键值的最大值?
- 10. 如何排序并从数组中获取最小值和最大值?
- 11. 如何在swift中从2个csv文件中获取数据
- 12. 如何从php中的数据库中获取最大值?
- 13. 如何从多个数组中获取最后一个数组?
- 14. 如何从F#中的数组中获取最小值?
- 15. 获取PHP数组中的最小值和最大值
- 16. PHP 2数组从两个数组中获取值
- 17. c#从数组中获取最小值
- 18. 从数组中获取最小值
- 19. 从多维数组中的元素获取最大值?
- 20. 如何从Key Json中逐个获取值(数组中的值)?
- 21. 如何在Android中获取RangeSeekBar中的当前最小值和最大值?
- 22. 如何从jQueryUI Slider中获取最小值和最大值
- 23. 如何从链表中获取最小值或最大值?
- 24. 以编程方式获取SQL中float的最大值
- 25. 如何在javascript中获取数组的最小值和最大值?
- 26. 如何获取String []数组的最大/最小值?
- 27. 从未排序数组中获取最大值
- 28. 如何获取从第一行到当前行的最大值
- 29. 如何从php数组中获取值
- 30. 如何从2d数组中获取值? -
排序并获得第2? –