登录
首页 >  Golang >  Go问答

GO Lang 中具有类似 Python 嵌套字典的功能

来源:stackoverflow

时间:2024-02-12 23:36:23 432浏览 收藏

Golang不知道大家是否熟悉?今天我将给大家介绍《GO Lang 中具有类似 Python 嵌套字典的功能》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!

问题内容

免责声明:我最近开始学习 go。所以如果你们觉得这个问题很天真,请原谅我。

我基本上是在尝试使用映射在 go 中复制类似 python 的嵌套字典行为。

这是一个简单的 python 函数,我想将其转换为 go

def generate_chart():

    outcome_chart = {}

    a_list = ['a1', 'a2', 'a3', 'a4', ....., 'a10']
    b_list = ['b1', 'b2', 'b3', 'b4', ....., 'b10']
    c_list = ['c1', 'c2', 'c3', 'c4', ....., 'c10']

    for a in a_list:
        for b in b_list:
            for c in c_list:
                outcome_chart[a] = outcome_chart.get(a) or {}
                outcome_chart[a][b] = outcome_chart[a].get(b) or {}
                result = "some string from external source, would be different on each request" 
                outcome_chart[a][b].update({c: result})

    return outcome_chart

这个想法很简单,我有 3 个字符串列表。并尝试为不同的组合创建地图/图表。 该函数运行后的示例

{
   'a1': {'b1': {'c1': 'somestring'}},
   ...
   'a2': {'b1': {'c3': 'someotherstring'}},
   ...
   'a6': {'b7': {'c2': 'string'}},
   ...
 }

现在,基于我对 go 的了解很少,我已经想出了这段代码,但它并不完整,因为我陷入了中间。

func generateChart() map[string]map[string]map[string]string {
    var result string
    var outcome_chart = map[string]map[string]map[string]string{}
    for _, a := range A_LIST {
        for _, b := range B_LIST {
            for _, c := range C_LIST {
                
                result = "some string from external source, would be different on each request" 

                if _, ok := outcome_chart[a]; ok {
                    if _, found := outcome_chart[a][b]; found {
                        if _, in := outcome_chart[a][b][c]; in {
                            outcome_chart[a][b][c] = result
                        } else {
                            outcome_chart[a][b] = map[string]string{c: result}
                        }
                    } else {
                        outcome_chart[a] = map[string]map[string]string{b: {c: result}}
                    }
                    outcome_chart[a] = # got stuck here, dont know if the previous lines are also correct.
                }
            }
        }
    }

    return outcome_chart

我可能以完全错误的方式做这件事。可能有一个使用结构或/和接口的解决方案,但正如我提到的,我仍在尝试将自己暴露在 go 世界中。因此,欢迎所有反馈/建议/意见。

ps:这只是 3 级嵌套,因此仍然可以扩展以手动编写 3 级映射,但是我们如何才能实现更多嵌套级别的类似功能,比如 100 或 1000 甚至无限。


正确答案


go 中的等效项:

func generatechart() map[string]map[string]map[string]string {
    var outcome_chart = map[string]map[string]map[string]string{}
    for _, a := range a_list {
        for _, b := range b_list {
            for _, c := range c_list {
                if _, ok := outcome_chart[a]; !ok {
                    outcome_chart[a] = map[string]map[string]string{}
                }

                if _, ok := outcome_chart[a][b]; !ok {
                    outcome_chart[a][b] = map[string]string{}
                }

                result := "some string from external source, would be different on each request"
                outcome_chart[a][b][c] = result
            }
        }
    }
    return outcome_chart
}

实现相同目标的更经济的方法:

func generatechart() map[string]map[string]map[string]string {
    var outcome_chart = map[string]map[string]map[string]string{}
    for _, a := range a_list {
        if _, ok := outcome_chart[a]; !ok {
            outcome_chart[a] = map[string]map[string]string{}
        }

        for _, b := range b_list {
            if _, ok := outcome_chart[a][b]; !ok {
                outcome_chart[a][b] = map[string]string{}
            }

            for _, c := range c_list {
                result := "some string from external source, would be different on each request"
                outcome_chart[a][b][c] = result
            }
        }
    }

    return outcome_chart
}

对于更嵌套的东西,为了方便起见,您可以使用声明的递归类型,并撒上一些方法:

type Chart struct {
    M map[string]*Chart
    V string
}

func newChart() *Chart {
    return &Chart{M: make(map[string]*Chart)}
}

func (c *Chart) addIfEmpty(key string) *Chart {
    if _, ok := c.M[key]; !ok {
        c.M[key] = newChart()
    }
    return c.M[key]
}

func generateChart() *Chart {
    var chart = newChart()
    for _, a := range A_LIST {
        aChart := chart.addIfEmpty(a)
        for _, b := range B_LIST {
            bChart := aChart.addIfEmpty(b)
            for _, c := range C_LIST {
                result := "some string from external source, would be different on each request"
                bChart.addIfEmpty(c).V = result
            }
        }
    }
    return chart
}

好了,本文到此结束,带大家了解了《GO Lang 中具有类似 Python 嵌套字典的功能》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>