登录
首页 >  Golang >  Go问答

将两个切片合并到映射的方法

来源:stackoverflow

时间:2024-02-14 12:36:24 450浏览 收藏

怎么入门Golang编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《将两个切片合并到映射的方法》,涉及到,有需要的可以收藏一下

问题内容

我有一个以切片形式返回 2 个变量的函数,我想将这两个切片作为键值对合并到一个映射中。问题是无法找到一种方法来单独迭代每个切片并将它们添加为键和值。 当前的实现正在迭代所有价格,并将每个价格添加到城镇键中。

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    cities, prices := citiesAndPrices()
    towns := groupSlices(cities, prices)
    for cities := range towns {

        fmt.Println(cities, prices)
    }

}

func citiesAndPrices() ([]string, []int) {
    rand.Seed(time.Now().UnixMilli())
    cityChoices := []string{"Berlin", "Moscow", "Chicago", "Tokyo", "London"}
    dataPointCount := 100
    cities := make([]string, dataPointCount)
    for i := range cities {
        cities[i] = cityChoices[rand.Intn(len(cityChoices))]
    }
    prices := make([]int, dataPointCount) 
    for i := range prices {
        prices[i] = rand.Intn(100)
    }
    return cities, prices
}

func groupSlices([]string, []int) map[string]int {
    cities, prices := citiesAndPrices()
    towns := make(map[string]int)
    for _, cities := range cities {
        for _, prices := range prices {
            towns[cities] = prices
            break
        }
    }
    return towns
}

正确答案


您必须将 groupslice 函数返回为 map[string][]int

这是给您的提示:

seed 与 rand.seed 方法不同,可以安全地并发使用。 source.

所以你可以调用它一次,而不是在每个调用函数上。这将加快您的应用程序的速度。

我认为这就是您想要的代码。

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    rand.Seed(time.Now().UnixNano())
    cities, prices := citiesAndPrices()
    towns := groupSlices(cities, prices)
    fmt.Println()
    total := 0
    for c, p := range towns {
        fmt.Println(c, p)
        total += len(p)
    }
    fmt.Println()
    fmt.Println("total must 200, found :", total) // total must be 200
}

func citiesAndPrices() ([]string, []int) {
    cityChoices := []string{"Berlin", "Moscow", "Chicago", "Tokyo", "London"}
    dataPointCount := 100
    cities := make([]string, dataPointCount)
    prices := make([]int, dataPointCount)
    for i := range cities {
        cities[i] = cityChoices[rand.Intn(len(cityChoices))]
        prices[i] = rand.Intn(100)
    }
    fmt.Println("inside citiesAndPrices func")
    fmt.Println(cities)
    fmt.Println(prices)
    fmt.Println("done run citiesAndPrices func\n")
    return cities, prices
}

func groupSlices(c1 []string, p1 []int) map[string][]int {
    c2, p2 := citiesAndPrices()
    towns := make(map[string][]int)
    for i, t := range c1 {
        // this is for cities1 and price1
        if towns[t] == nil {
            towns[t] = make([]int, 0)
        }
        towns[t] = append(towns[t], p1[i])

        // this is for cities2 and price2
        if towns[c2[i]] == nil {
            towns[c2[i]] = make([]int, 0)
        }
        towns[c2[i]] = append(towns[c2[i]], p2[i])
    }
    return towns
}

终于介绍完啦!小伙伴们,这篇关于《将两个切片合并到映射的方法》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

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