gpt4 book ai didi

arrays - 创建字典 : Assigning an Array of Ints as Keys to another array

转载 作者:搜寻专家 更新时间:2023-11-01 05:51:01 25 4
gpt4 key购买 nike

我有一个数组数组和一个整数数组,我想创建一个字典,其中整数数组元素作为数组数组中元素的键。

我尝试了很多迭代方法,但运气不佳。有什么想法或想法吗?

var populationArray = [[98, 8, 45, 34, 56], [9, 13, 65, 4, 90], [24, 5, 4, 56, 88], [3, 55, 22, 19, 10], [8, 33, 26, 93, 16], [31, 38, 92, 70, 36], [9, 39, 15, 14, 66]]

var IntegerKeys = [17, 41, 10, 34, 5, 85, 87]

var Dictionary : [Int: [Int]] = [:]

最佳答案

试试这个:

var populationArray = [[98, 8, 45, 34, 56], [9, 13, 65, 4, 90], [24, 5, 4, 56, 88], [3, 55, 22, 19, 10], [8, 33, 26, 93, 16], [31, 38, 92, 70, 36], [9, 39, 15, 14, 66]]

var IntegerKeys = [17, 41, 10, 34, 5, 85, 87]

var Dictionary = [Int: [Int]]()

// Swift 2.0
for (index, key) in IntegerKeys.enumerate() {
Dictionary[key] = populationArray[index]
}

// Swift 1.2
for (index, key) in enumerate(IntegerKeys) {
Dictionary[key] = populationArray[index]
}

关于arrays - 创建字典 : Assigning an Array of Ints as Keys to another array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32297587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com