将一个方法中的两个切片合并成一个映射。

huangapple go评论118阅读模式
英文:

Merging 2 slices from a method into a map

问题

我有一个函数,返回两个变量,形式为切片,我想将这两个切片合并成一个键值对的映射。问题是我找不到一种方法来分别迭代每个切片,并将它们作为键和值添加。

当前的实现是遍历所有的价格,并将每个价格添加到城市键中。

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "time"
  6. )
  7. func main() {
  8. cities, prices := citiesAndPrices()
  9. towns := groupSlices(cities, prices)
  10. for cities := range towns {
  11. fmt.Println(cities, prices)
  12. }
  13. }
  14. func citiesAndPrices() ([]string, []int) {
  15. rand.Seed(time.Now().UnixMilli())
  16. cityChoices := []string{"Berlin", "Moscow", "Chicago", "Tokyo", "London"}
  17. dataPointCount := 100
  18. cities := make([]string, dataPointCount)
  19. for i := range cities {
  20. cities[i] = cityChoices[rand.Intn(len(cityChoices))]
  21. }
  22. prices := make([]int, dataPointCount)
  23. for i := range prices {
  24. prices[i] = rand.Intn(100)
  25. }
  26. return cities, prices
  27. }
  28. func groupSlices([]string, []int) map[string]int {
  29. cities, prices := citiesAndPrices()
  30. towns := make(map[string]int)
  31. for _, cities := range cities {
  32. for _, prices := range prices {
  33. towns[cities] = prices
  34. break
  35. }
  36. }
  37. return towns
  38. }
英文:

I have a function that returns 2 variables in a form of slices and I want to merge the two slices into a map as key-value pairs. The issue is that can't find a way to iterate through each slice separately and add them as a key and a value.
The current implementation is iterating through all the prices and it's adding every price to the town key.

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "time"
  6. )
  7. func main() {
  8. cities, prices := citiesAndPrices()
  9. towns := groupSlices(cities, prices)
  10. for cities := range towns {
  11. fmt.Println(cities, prices)
  12. }
  13. }
  14. func citiesAndPrices() ([]string, []int) {
  15. rand.Seed(time.Now().UnixMilli())
  16. cityChoices := []string{"Berlin", "Moscow", "Chicago", "Tokyo", "London"}
  17. dataPointCount := 100
  18. cities := make([]string, dataPointCount)
  19. for i := range cities {
  20. cities[i] = cityChoices[rand.Intn(len(cityChoices))]
  21. }
  22. prices := make([]int, dataPointCount)
  23. for i := range prices {
  24. prices[i] = rand.Intn(100)
  25. }
  26. return cities, prices
  27. }
  28. func groupSlices([]string, []int) map[string]int {
  29. cities, prices := citiesAndPrices()
  30. towns := make(map[string]int)
  31. for _, cities := range cities {
  32. for _, prices := range prices {
  33. towns[cities] = prices
  34. break
  35. }
  36. }
  37. return towns
  38. }

答案1

得分: 0

你必须将groupSlice函数返回为map[string][]int类型。

这是一个提示:

Seed方法与Rand.Seed方法不同,它在并发使用时是安全的。来源

因此,你可以只调用一次Seed方法,而不是在每次调用函数时都调用它。这将加快你的应用程序的速度。

我认为这是你想要的代码。

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "time"
  6. )
  7. func main() {
  8. rand.Seed(time.Now().UnixNano())
  9. cities, prices := citiesAndPrices()
  10. towns := groupSlices(cities, prices)
  11. fmt.Println()
  12. total := 0
  13. for c, p := range towns {
  14. fmt.Println(c, p)
  15. total += len(p)
  16. }
  17. fmt.Println()
  18. fmt.Println("total must 200, found :", total) // total must be 200
  19. }
  20. func citiesAndPrices() ([]string, []int) {
  21. cityChoices := []string{"Berlin", "Moscow", "Chicago", "Tokyo", "London"}
  22. dataPointCount := 100
  23. cities := make([]string, dataPointCount)
  24. prices := make([]int, dataPointCount)
  25. for i := range cities {
  26. cities[i] = cityChoices[rand.Intn(len(cityChoices))]
  27. prices[i] = rand.Intn(100)
  28. }
  29. fmt.Println("inside citiesAndPrices func")
  30. fmt.Println(cities)
  31. fmt.Println(prices)
  32. fmt.Println("done run citiesAndPrices func\n")
  33. return cities, prices
  34. }
  35. func groupSlices(c1 []string, p1 []int) map[string][]int {
  36. c2, p2 := citiesAndPrices()
  37. towns := make(map[string][]int)
  38. for i, t := range c1 {
  39. // this is for cities1 and price1
  40. if towns[t] == nil {
  41. towns[t] = make([]int, 0)
  42. }
  43. towns[t] = append(towns[t], p1[i])
  44. // this is for cities2 and price2
  45. if towns[c2[i]] == nil {
  46. towns[c2[i]] = make([]int, 0)
  47. }
  48. towns[c2[i]] = append(towns[c2[i]], p2[i])
  49. }
  50. return towns
  51. }
英文:

You must return groupSlice function as map[string][]int

This is a tips for you:

> Seed, unlike the Rand.Seed method, is safe for concurrent use. source.

So You can call it once, not on every call function. This will speed up your app.

I think this is the code that what you want.

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "time"
  6. )
  7. func main() {
  8. rand.Seed(time.Now().UnixNano())
  9. cities, prices := citiesAndPrices()
  10. towns := groupSlices(cities, prices)
  11. fmt.Println()
  12. total := 0
  13. for c, p := range towns {
  14. fmt.Println(c, p)
  15. total += len(p)
  16. }
  17. fmt.Println()
  18. fmt.Println("total must 200, found :", total) // total must be 200
  19. }
  20. func citiesAndPrices() ([]string, []int) {
  21. cityChoices := []string{"Berlin", "Moscow", "Chicago", "Tokyo", "London"}
  22. dataPointCount := 100
  23. cities := make([]string, dataPointCount)
  24. prices := make([]int, dataPointCount)
  25. for i := range cities {
  26. cities[i] = cityChoices[rand.Intn(len(cityChoices))]
  27. prices[i] = rand.Intn(100)
  28. }
  29. fmt.Println("inside citiesAndPrices func")
  30. fmt.Println(cities)
  31. fmt.Println(prices)
  32. fmt.Println("done run citiesAndPrices func\n")
  33. return cities, prices
  34. }
  35. func groupSlices(c1 []string, p1 []int) map[string][]int {
  36. c2, p2 := citiesAndPrices()
  37. towns := make(map[string][]int)
  38. for i, t := range c1 {
  39. // this is for cities1 and price1
  40. if towns[t] == nil {
  41. towns[t] = make([]int, 0)
  42. }
  43. towns[t] = append(towns[t], p1[i])
  44. // this is for cities2 and price2
  45. if towns[c2[i]] == nil {
  46. towns[c2[i]] = make([]int, 0)
  47. }
  48. towns[c2[i]] = append(towns[c2[i]], p2[i])
  49. }
  50. return towns
  51. }

huangapple
  • 本文由 发表于 2022年2月25日 05:05:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/71258244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定