dqjcb132285 2019-02-19 00:58
浏览 23
已采纳

如何将键值映射转换为int的切片

i'm trying to convert key-value map to slice of 2 value, for example :

slices := make(map[int64]int64)
slices[int64(521)] = int64(4)
slices[int64(528)] = int64(8)
// how do i convert that to become
// [[521, 4], [528, 8]]

i'm thinking about each all those key-value then create slice for that, but is there any simple code to do that ?

  • 写回答

1条回答 默认 最新

  • doubi8383 2019-02-19 01:05
    关注
    package main
    
    import "fmt"
    
    func main() {
        //create a map
        m := map[int64]int64{512: 8, 513: 9, 234: 9, 392: 0}
    
        //create a slice to hold required values
        s := make([][]int64, 0)
    
        //range over map `m` to append to slice `s`
        for k, v := range m {
    
            // append each element, with a new slice []int64{k, v}
            s = append(s, []int64{k, v})
        }
    
        fmt.Println(s)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制