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

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

Merging 2 slices from a method into a map

问题

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

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

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
}
英文:

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.

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
}

答案1

得分: 0

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

这是一个提示:

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

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

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

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
}

英文:

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.

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
}

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:

确定