dtzk85937
2017-05-06 01:55
浏览 52
已采纳

去一维数组结合二维数组与追加

I have two one dimensional array and I want to combine the two single arrays into one multi dimensional array with append.

How would this be done for the fastest in go?

val time []int64
val value []float64

val 2darray [][]int64, float64

Would append be the best way to do this in go?

图片转代码服务由CSDN问答提供 功能建议

我有两个一维数组,并且我想用将两个单个数组组合成一个多维数组 附加即可。

如何做到最快呢?

  val时间[] int64 
val值[] float64 
 \  nval 2darray [] [] int64,float64 
   
 
 

append是执行此操作的最佳方法吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doufud21086 2017-05-07 20:42
    已采纳

    Here's an example of how it can be done:

    package main
    
    import (
        "fmt"
    )
    
    type TimeAndValue struct {
        time  int64
        value float64
    }
    
    func main() {
        times := []int64{0, 1, 2, 3, 4}
        values := []float64{1.23, 2.34, 3.45, 4.56, 5.67}
    
        timesAndValues := zip(times, values)
    
        fmt.Println(timesAndValues)
    }
    
    func zip(ts []int64, vs []float64) []TimeAndValue {
        if len(ts) != len(vs) {
            panic("not same length")
        }
    
        var res []TimeAndValue
        for i, t := range ts {
            res = append(res, TimeAndValue{time: t, value: vs[i]})
        }
    
        return res
    }
    

    https://play.golang.org/p/1OWJ1HG1XL

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题