在Golang中反转< value,key>格式的Map

xt0899hw  于 2023-06-19  发布在  Go
关注(0)|答案(6)|浏览(279)

我的程序有一个map,看起来像下面这样:

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

我想把它颠倒过来,这样它就可以读到下面的内容:

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

值中不存在重复项。而且,我的Map很小-大约200键。我没有找到任何内置的方法来反转这样的Map。有什么办法能快点吗?我对空间的复杂性不太在意,但解决方案需要快速。
谢谢

i5desfxk

i5desfxk1#

您可以编写一个for循环来迭代原始map的键值对,并将它们放入新的map中(请参见函数reverseMap
Code@https:play.golang.org/p/5y1gABTpdb8

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-case,比如fruitMap

roejwanj

roejwanj2#

其他答案提供了基于直接处理Map的简单解决方案。
另一种解决方案是将双向Map封装为自包含的实用程序,其优点是您可以为其编写全面的单元测试,然后能够通过简单的API依赖它正确操作。
下面是my example implementatio n(它是不完整的,还没有必要的单元测试):
主程序包

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
}
wfauudbj

wfauudbj3#

你是对的,没有内置的东西来实现这一点,但它真的很简单:

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]

playground上查看。如果这是常见的,你总是可以提取到你自己的函数。

hc8w905p

hc8w905p4#

没有一个内置的函数可以做到这一点,但是用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

xfb7svmp

xfb7svmp5#

另一种选择是使用定制类型并实现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())
}
f0brbegy

f0brbegy6#

扩展@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
}

相关问题