我有一个数组数组和一个整数数组,并希望创建一个字典,其中Integers数组元素作为数组数组中元素的键。
我尝试了许多迭代方法而没有太多运气。有什么想法或想法吗?
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]] = [:]
答案 0 :(得分:5)
试试这个:
Install failed. Rolling back...
答案 1 :(得分:3)
@ ZoffDino的答案确实有效,但如果populationArray
包含的元素少于integerKeys
,则会崩溃。我提议的方法没有这个缺陷:
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]] = [:]
for (key, value) in zip(IntegerKeys, populationArray) {
dictionary[key] = value
}