我正在尝试获取数组字典中的最大值,我还需要获取该数组的键。
这就是我获得最大价值的方法:

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 9, 16, 25]
]
var largest = 0

for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number
        }
    }
}

最佳答案

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 9, 16, 25],
]
var largest = 0
var largest_kind : String? =nil
for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number
            largest_kind = kind
        }
    }
}

关于arrays - 如何从数组字典中的整数数组中获取最大值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42158696/

10-10 21:24