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

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

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?

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题