gpt4 book ai didi

go - 枢轴映射[键]反转未还原正确的值

转载 作者:行者123 更新时间:2023-12-01 22:41:40 26 4
gpt4 key购买 nike

扩展https://github.com/golang/go/issues/39242 ...

我试图通过附加外部 slice 来展平内部 slice ,然后反转通过附加内部 slice 来展平外部 slice 的输出

https://play.golang.org/p/2tubtql5UwB

package main

import (
"fmt"
)

func main() {
inner := make([]interface{}, 0)
inner = append(inner, map[string]interface{}{"id": "1"})
inner = append(inner, map[string]interface{}{"id": "2"})
inner = append(inner, map[string]interface{}{"id": "3"})

outer := make([]interface{}, 0)
outer = append(outer, map[string]interface{}{"id": "A", "inner": inner})
fmt.Println(outer)

// flatten inner slice into 1 level
pivotInners := make([]interface{}, 0)
for _, itemOuter := range outer {
itemOuterMap := itemOuter.(map[string]interface{})
innerSlice := itemOuterMap["inner"].([]interface{})
delete(itemOuterMap, "inner")
for _, itemInner := range innerSlice {
itemInnerMap := itemInner.(map[string]interface{})
itemInnerMap["outer"] = itemOuterMap
pivotInners = append(pivotInners, itemInner)
}
}
fmt.Println(pivotInners)

// flatten outer slice to 1 level
pivotOuters := make([]interface{}, 0)
for _, pivotInner := range pivotInners {
pivotInnerMap := pivotInner.(map[string]interface{})
outerX := pivotInnerMap["outer"].(map[string]interface{})
delete(pivotInnerMap, "outer")

// dump row
outerX["inner"] = pivotInnerMap
fmt.Println(outerX)

pivotOuters = append(pivotOuters, outerX)
}

// dump collection
fmt.Println(pivotOuters)
}

内部 slice
map[id:1]
map[id:2]
map[id:3]

外层 slice
[map[id:A inner:[map[id:1] map[id:2] map[id:3]]]]

内枢轴
[map[id:1 outer:map[id:A]] map[id:2 outer:map[id:A]] map[id:3 outer:map[id:A]]]

我期待

外枢轴
[map[id:A inner:map[id:1]] map[id:A inner:map[id:2]] map[id:A inner:map[id:3]]]

但是得到了

外枢轴

如果我回显内部行,它将返回正确的值
map[id:A inner:map[id:1]]
map[id:A inner:map[id:2]]
map[id:A inner:map[id:3]]

但附加后, slice 仅获得最后一行
[map[id:A inner:map[id:3]] map[id:A inner:map[id:3]] map[id:A inner:map[id:3]]]

有人有线索吗?

最佳答案

解决方案是添加一个副本,以便每个内部 map 都不会引用相同的外部 map 。

https://play.golang.org/p/lwVuvTuwVVc

package main

import (
"fmt"
)

func main() {
inner := make([]interface{}, 0)
inner = append(inner, map[string]interface{}{"id": "1"})
inner = append(inner, map[string]interface{}{"id": "2"})
inner = append(inner, map[string]interface{}{"id": "3"})

outer := make([]interface{}, 0)
outer = append(outer, map[string]interface{}{"id": "A", "inner": inner})
fmt.Println(outer)

// flatten inner slice into 1 level
pivotInners := make([]interface{}, 0)
for _, itemOuter := range outer {
itemOuterMap := itemOuter.(map[string]interface{})
innerSlice := itemOuterMap["inner"].([]interface{})
delete(itemOuterMap, "inner")
for _, itemInner := range innerSlice {
itemInnerMap := itemInner.(map[string]interface{})
outerCopy := make(map[string]interface{}, len(itemOuterMap))
for k,v := range itemOuterMap {
outerCopy[k] = v
}
itemInnerMap["outer"] = outerCopy
pivotInners = append(pivotInners, itemInner)
}
}
fmt.Println(pivotInners)

// flatten outer slice to 1 level
pivotOuters := make([]interface{}, 0)
for _, pivotInner := range pivotInners {
pivotInnerMap := pivotInner.(map[string]interface{})
outerX := pivotInnerMap["outer"].(map[string]interface{})
delete(pivotInnerMap, "outer")

// dump row
outerX["inner"] = pivotInnerMap
fmt.Println(outerX)

pivotOuters = append(pivotOuters, outerX)
}

// dump collection
fmt.Println(pivotOuters)
}

输出量
map[id:A inner:map[id:1]]
map[id:A inner:map[id:2]]
map[id:A inner:map[id:3]]
[map[id:A inner:map[id:1]] map[id:A inner:map[id:2]] map[id:A inner:map[id:3]]]

感谢 https://github.com/200sc回答了关于松弛的问题!

关于go - 枢轴映射[键]反转未还原正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62001552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com