我如何合并两个数组到一个字典? [英] How can I merge two arrays into a dictionary?

查看:1711
本文介绍了我如何合并两个数组到一个字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组:

    var identic = [String]()
    var linef = [String]()

我和数据追加他们。现在对于可用性的目的我的目标是他们都具有以下结构组合成一个字典

I've appended them with data. Now for usability purposes my goal is to combine them all into a dictionary with the following structure

FullStack = ["identic 1st value":"linef first value", "identic 2nd value":"linef 2nd value"]

我一直在浏览周围的网,无法找到一个可行的解决了这一点。

I've been browsing around the net and couldnt find a viable solution to this.

任何想法是极大的AP preciated。

Any ideas are greatly appreciated.

感谢您!

推荐答案

这听起来像一个工作 .enumerate()

var arrayOne: [String] = []
var arrayTwo: [String] = []

var dictionary: [String: String] = [:]

for (index, element) in arrayOne.enumerate()
{
    dictionary[element] = arrayTwo[index]
}

不过,如果你想在的办法,使用扩展:

extension Dictionary
{
    public init(keys: [Key], values: [Value])
    {
        precondition(keys.count == values.count)

        self.init()

        for (index, key) in keys.enumerate()
        {
            self[key] = values[index]
        }
    }
}

这篇关于我如何合并两个数组到一个字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆