比较Golang中的两张地图

时间:2018-09-30 20:49:39

标签: go hashmap

我有两张地图:

第一个:

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]

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

2 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,则应该可以进行以下操作:

步骤I。在第二张地图中反转键和值:reversed_map_2。假设这些值是唯一的。

第二步。循环遍历第一张地图中的值,现在您可以在reversed_map_2

中查找值

答案 1 :(得分:0)

我不太了解您的第二张地图。但是我对您的问题的理解是,您希望从地图中提取独特的元素。

我提取的结果基于值而不是键。它向您展示了如何比较两张地图,并以(键,值)的形式显示结果。

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

为了根据我使用的值显示匹配元素之间的明显区别。