go - 比较 Golang 中的两个 map

标签 go hashmap

我有两个 map :

第一个:

map[11:manufacturer 2:upc 5:short_description 10: 4:category 6: 7:url 
8:image 9: 0:name 1:mpn 3:sku]

第二个:

map[3:manufacturer 5:mpn 8:category_path 10:is_in_stock 2:final_price 
1:name 4:short_description 6:thumbnail 7:url 9:furniture_type 0:sku]
map[news_to_date:2014-10-20 00:00:00 url_key:zanbury-panel-storage- 
bedroom-set price:0 media_gallery:[map[value_id:507779 file:/b/2/b217- 
31-36-46-57-54s-95-92.jpg label:<nil> disabled:0 position:1] 
map[label:<nil> disabled:0 position:2 value_id:507777 file:/b/2/b217- 
57-54s-95_1.jpg] map[value_id:507778 file:/b/2/b217-57-detail_3.jpg 
label:<nil> disabled:0 position:3] map[value_id:507780 file:/b/2/b217- 
54s-detail_1.jpg label:<nil> disabled:0 position:4] 
map[file:/b/2/b217-handle_1.jpg label:<nil> disabled:0 position:5 
value_id:507781] map[value_id:507782 file:/b/2/b217-92-sw_1.jpg label: 
<nil> disabled:0 position:6] map[value_id:507783 file:/b/2/b217-31-36- 
sw_-_copy_2_.jpg label:<nil> disabled:0 position:7] 
map[value_id:507784 file:/b/2/b217-46-sw_1.jpg label:<nil> disabled:0 
position:8]] sku_8:<nil> sku_1:<nil> shipment_type:0 url_path:zanbury- 
panel-storage-bedroom-set.htm custom_design:<nil> sku_5:<nil> upc: 
<nil> special_from_date:<nil> mk_expecdate:<nil> sku_type:1 
has_options:1 price_view:0 jet_price:<nil> categories:[11 10809]

我如何比较两者并从第二个中选择与第一个匹配的值。

最佳答案

我不太了解您的第二张 map 。但我对你的问题的理解是你想从 map 中提取独特的元素。

我提取的结果是基于值而不是键。它向您展示了如何比较 2 个 map 并以(键,值)的形式获得结果。

func unique(w http.ResponseWriter, r *http.Request) {
    m1 := map[int]string{
        11: "manufacturer",
        2:  "upc",
        5:  "short_description",
        10: "",
        4:  "category",
        6:  " ",
        7:  "url",
        8:  "image",
        9:  "",
        0:  "name",
        1:  "mpn",
        3:  "sku",
    }
    m2 := map[int]string{
        3:  "manufacturer",
        5:  "mpn",
        8:  "category_path",
        10: "is_in_stock",
        2:  "final_price",
        1:  "name",
        4:  "short_description",
        6:  "thumbnail",
        7:  "url",
        9:  "furniture_type",
        0:  "sku",
    }

    for key, val := range m1 {
        found := false
        for key1, val1 := range m2 {
            if val == val1 {
                found = true
                fmt.Println("second map")
                fmt.Println("key:", key1, "val:", val1)
                break
            }
        }
        if found {
            fmt.Println("first map")
            fmt.Println("key:", key, "val:", val)
        }
    }
}

输出将是:

second map
key: 5 val: mpn
first map
key: 1 val: mpn
second map
key: 0 val: sku
first map
key: 3 val: sku
second map
key: 3 val: manufacturer
first map
key: 11 val: manufacturer
second map
key: 1 val: name
first map
key: 0 val: name
second map
key: 4 val: short_description
first map
key: 5 val: short_description
second map
key: 7 val: url
first map
key: 7 val: url

为了根据找到的值显示匹配元素之间的明显区别。

关于go - 比较 Golang 中的两个 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52582040/

相关文章:

java - 如何根据值以升序打印 HashMap<String, String> 的内容?

c - 数据在一个内存块中的 ANSI C 哈希表实现

java - HashMap 改变 Values 对象

go - 使用通配符匹配删除 s3 中的对象

go - Cloud BigTable 存储同一个 rowkey 的多个副本

go - 在golang中动态解码yaml

go - 使用 go-restful 和 swagger 显示 PUT 正文格式

go - 在HashiCorp库中搜索值

java - 如何对 MultiValuedMap 中包含的 Double ArrayList 进行排序

java - 在 Hashmap 的 Hashmap 中查找最大值