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 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式