在 Golang 中以 <值、键> 格式反转地图



我的程序有一个映射,如下所示:

fruit_map := map[string]string {
    "apple": "likey",
    "orange": "no likey",
}

我想把它倒过来,让它读如下:

{
    "likey": "apple",
    "no likey": "orange",
}

值中不存在重复项。此外,我的地图很小,大约有200把钥匙。我没有找到任何内置的方法来反转这样的映射。有什么方法可以这么快吗?我不太在意空间的复杂性,但解决方案需要快速。

谢谢。

您可以编写一个for循环来迭代原始映射的键值对,并将它们放在新映射中(请参见函数reverseMap

Code@https://play.golang.org/p/51gABTpdb8

package main
import (
    "fmt"
)
func main() {
    fruit_map := map[string]string{
        "apple":  "likey",
        "orange": "no likey",
    }
    reversedMap := reverseMap(fruit_map)
    fmt.Println(reversedMap)
}
func reverseMap(m map[string]string) map[string]string {
    n := make(map[string]string, len(m))
    for k, v := range m {
        n[v] = k
    }
    return n
}

输出:

map[likey:apple no likey:orange]

顺便说一句,像fruit_map这样命名go变量是不习惯的,你真的应该使用camel大小写,比如fruitMap

其他答案提供了基于直接处理映射的简单解决方案。

另一种解决方案是将双向映射封装为一个自包含的实用程序,其优点是您可以为其编写完整的单元测试,然后能够依赖它通过简单的API正确操作。

以下是我的示例实现(它是不完整的,还没有必要的单元测试):

包装主

import (
    "fmt"
)
func main() {
    biMap := NewBiMap()
    biMap.Put("apple", "likey")
    biMap.Put("orange", "no likey")
    v, _ := biMap.GetByValue("no likey")
    fmt.Println(v)
}
type BiMap struct {
    ab map[string]string
    ba map[string]string
}
func NewBiMap() *BiMap {
    return &BiMap{make(map[string]string), make(map[string]string)}
}
func (m *BiMap) Put(key, value string) *BiMap {
    m.ab[key] = value
    m.ba[value] = key
    return m
}
func (m *BiMap) GetByKey(key string) (value string, exists bool) {
    value, exists = m.ab[key]
    return
}
func (m *BiMap) GetByValue(value string) (key string, exists bool) {
    key, exists = m.ba[value]
    return
}
func (m *BiMap) Len() int {
    return len(m.ab)
}
func (m *BiMap) DeleteKey(key string) *BiMap {
    value, exists := m.ab[key]
    if exists {
        delete(m.ab, key)
        delete(m.ba, value)
    }
    return m
}
func (m *BiMap) DeleteValue(value string) *BiMap {
    key, exists := m.ba[value]
    if exists {
        delete(m.ab, key)
        delete(m.ba, value)
    }
    return m
}

你是对的,没有任何内置功能可以实现这一点,但它非常简单:

package main
import "fmt"
func main() {
    fruit_map := map[string]string{
        "apple":  "likey",
        "orange": "no likey",
    }
    //create your new empty map that will hold your reversed contents.
    reversed_fruit_map := make(map[string]string)
    for k, v := range fruit_map{
        reversed_fruit_map[v] = k
    }
    fmt.Println(reversed_fruit_map)
}

这会输出以下内容:

map[likey:apple no likey:orange]

在操场上看看。如果这很常见,你总是可以提取到你自己的函数中。

没有内置函数可以做到这一点,但使用for循环就足够简单了。

fruit_map := map[string]string {
    "apple": "likey",
    "orange": "no likey",
}
reversed_map := make(map[string]string)
for key,value := range fruit_map {
    reversed_map[value] = key
}

请参阅:http://play.golang.org/p/BQjqUsf9aU

使用实现了Reverse()方法的自定义类型是另一种选择。

package main
import "fmt"
type (
    MapA map[string]string
    MapB map[int]string
)
// Reverse returns a reverse map of the MapA object.
func (m MapA) Reverse() map[string]string {
    n := make(map[string]string, len(m))
    for k, v := range m {
        n[v] = k
    }
    return n
}
// Reverse returns a reverse map of the MapB object.
func (m MapB) Reverse() map[string]int {
    n := make(map[string]int, len(m))
    for k, v := range m {
        n[v] = k
    }
    return n
}
func main() {
    myMapA := &MapA{
        "apple":  "likey",
        "orange": "no likey",
    }
    myMapB := &MapB{
        10: "likey",
        5:  "not sure",
        0:  "no likey",
    }
    fmt.Println(myMapA.Reverse())
    fmt.Println(myMapB.Reverse())
}

用通用扩展@Dave C的答案

package main
import (
    "fmt"
)
func main() {
    myMap := map[int]string{
        1: "one",
        2: "two",
        3: "three",
    }
    result := reverseMap(myMap)
    fmt.Println(result)
}
func reverseMap[M ~map[K]V, K comparable, V comparable](m M) map[V]K {
    reversedMap := make(map[V]K)
    for key, value := range m {
        reversedMap[value] = key
    }
    return reversedMap
}

最新更新