比较GO语言中的地图值

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

Comparing the values of maps in GO

问题

我必须比较两个类型为'map[string]float64'的映射的值(而不是键)。

映射的内容是:map1[ABCD:300 PQRS:400]map2[ABCD:30 PQRS:40]
现在我要进行一个检查,如果value(map1)/value(map2) >= 1(例如300/30=10>1),那么执行某些操作。

在GO语言中,我该如何实现这个功能?
谢谢。

我尝试了以下代码:

for key := range m2 {
    for k := range m1 {
        temp := m1[k] / m2[key]
        fmt.Println("temp*******", temp)
    }
}
英文:

I have to compare the values (not keys) of two maps of type 'map[string]float64'.

The contents of the maps are : map1[ABCD:300 PQRS:400] and map2[ABCD:30 PQRS:40]
No I do a check like if value(map1)/value(map2) > = 1 (like 300/30=10>1), then do something.

How can I achieve this in GO ?
TIA.

i tried something like this :

for key := range m2{
			for k := range m1{
				temp := m1[k] / m2[key]
				fmt.Println("temp*******", temp)
			}
		}

答案1

得分: 2

Playground工作示例

解析主要的for循环:

for key, value1 := range m1 {
    if value2, ok := m2[key]; ok {
        fmt.Printf("%f / %f = %f\n", value1, value2, value1/value2)
        if value2 != 0 && value1/value2 > 1 {
            fmt.Println("大于1!")
        } else {
            fmt.Println("不大于1!")
        }
    }
}

首先,我使用range来遍历m1中的每个条目,以获取值和键,因为我们需要两者。

其次,使用value2, ok := m2[key]逗号ok语法,我既可以找出给定键的第二个映射的关联值,又可以确保在周围的if ... ; ok中存在该映射条目。根据您对问题的描述,不需要检查第二个映射的每个元素,只需要检查与第一个映射共享键的元素。这个检查很重要,因为在Go中,当检查一个不存在的键时,它不会抛出错误,而是静默地返回变量的零值--在这种情况下,这可能意味着后面要除以零。

第三,只是一些格式化的打印行,以帮助查看发生了什么,真正重要的是非常简单的if value1/value2 > 1语句。请注意,我还在我的playground链接中添加了一些不满足要求的映射条目,以帮助演示行为是否正确。

另一个答案中链接的“go maps in action”博文确实是使用go处理映射的很好的资源。

英文:

Playground working example

Breaking down the main for loop:

for key, value1 := range m1 {
	if value2, ok := m2[key]; ok {
		fmt.Printf("%f / %f = %f\n", value1, value2, value1/value2)
		if value2 != 0 && value1/value2 > 1 {
			fmt.Println("Greater than 1!")
		} else {
			fmt.Println("Not greater than 1!")
		}
	}
}

First I use range to pull out both a value and a key for every entry in m1, since we need both.

Second, using the comma ok syntax of value2, ok := m2[key], I'm both finding out what the associated value of the second map is for the given key, and also ensuring that that map entry exists when the surrounding if ... ; ok. From your description of the problem, it isn't necessary to check every element of the second map, only those that share keys with the first. It's important we make this check, since go doesn't throw errors when checking a map for a non-existent key, it just silently returns the zero-value of the variable--in this scenario that could mean dividing by zero later on.

Third, well, just some formatted printlines to help see what's happening, the real juice is the very simple if value1/value2 > 1 statement. Note that I also added in my playground link some map entries that don't fulfill the requirement to help demonstrate that the behavior is correct.

The "go maps in action" blog article linked in the other answer is indeed a great source for working with maps in go.

huangapple
  • 本文由 发表于 2015年9月30日 13:43:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/32858611.html
匿名

发表评论

匿名网友

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

确定