将2数组合并为字典

时间:2019-04-10 09:42:06

标签: ios swift

我想合并2个数组。

arr1 = ["a", "b", "c"]
arr2 = ["1", "2", "3"]

我想让它成为:

"a" = "1"
"b" = "2"
"c" = "3"

因此,如果我在标签1中调用值“ 1”,则意味着也像字典或索引一样将“ a”值也调用到标签2中。

2 个答案:

答案 0 :(得分:4)

就是这样:

SELECT (
        SELECT TOP 1 [local]
        FROM tbl
        WHERE [local] IS NOT NULL
        ORDER BY [id] DESC
        ) AS [local]
    ,(
        SELECT TOP 1 [roaming]
        FROM tbl
        WHERE [roaming] IS NOT NULL
        ORDER BY [id] DESC
        ) AS [roaming]
    ,(
        SELECT TOP 1 [rent]
        FROM tbl
        WHERE [rent] IS NOT NULL
        ORDER BY [id] DESC
        ) AS [rent]

答案 1 :(得分:0)

这是一个行代码

let dict = zip(["a", "b", "c"], ["1", "2", "3"]).compactMap{[$0.0:$0.1]}.reduce([:]) { $0.merging($1) { (current, _) in current } }       
print(dict)
  

[“ a”:“ 1”,“ b”:“ 2”,“ c”:“ 3”]

我建议您使用 user28434 的答案,该答案比我的更有效。我将答案作为第二好的解决方案