【问题标题】:how to group by multiple value and sum multiple value in golang [duplicate]如何在golang中按多个值分组并对多个值求和
【发布时间】:2023-03-28 02:10:01
【问题描述】:

我有这个代码

    type key struct {
        account  string
        quantity float64
    }
    type invoice_tag struct {
        account              string
        value_after_discount float64
        value                float64
        price                float64
        total_discount       float64
        discount             float64
        quantity             float64
    }

    invoice := []invoice_tag{{"Cash", 1024, 1024, 1, 0, 0, 1024}, {"Service Revenue", 0, 2048, 2, 0, 0, 1024}, {"Service Revenue", 0, 0, 0, 1024, 1, 1024}}
    m := map[key][5]float64{}
    for _, i := range invoice {
        m[key{i.account, i.quantity}] = [5]float64{i.value_after_discount, i.value, i.price, i.total_discount, i.discount}

    }
    fmt.Println(m)

我想按accountquantity 分组,并将value_after_discountvalue_after_discountvaluevaluepricepricetotal_discount 与@987654 和3 相加discountdiscount。并且输出应该是

map[{Cash 1024}:[1024 1024 1 0 0] {Service Revenue 1024}:[1024 2048 2 1024 1]]

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

【问题讨论】:

  • @icza 好的,但是如何对多个值求和?
  • 总结abc,你只需写a + b + c。请解释一下你不明白的地方。
  • @icza 我的意思是我想将a 与前一个ab 与前一个bc 与前一个c 相加键
  • 您将总和存储在地图中,并在下一次迭代中读取该值,添加新值并将新总和存储回地图中。比如:mymap[key] += value.

标签: dictionary go struct


【解决方案1】:

使用结构体作为具有accountquantity 字段的复合键。注意:浮点数比较可能会让你大吃一惊,如果quantity是整数,你应该使用整数(例如int)!

使用一个包含要求和的值的结构。为简单起见,我将为此使用invoice_tag,因为它包含所有必需的字段,但您也可以根据自己的喜好创建一个单独的结构。

我将在映射中存储一个指向该结构的指针,因此当我在每次迭代中更新它时,我不必存储新值。

例如:

m := map[key]*invoice_tag{}
for _, i := range invoice {
    k := key{i.account, i.quantity}
    sums := m[k]
    if sums == nil {
        sums = &invoice_tag{}
        m[k] = sums
    }
    sums.value_after_discount += i.value_after_discount
    sums.value += i.value
    sums.price += i.price
    sums.total_discount += i.total_discount
    sums.discount += i.discount
}

for k, v := range m {
    fmt.Printf("key: %v, sums: value_after_discount: %f, value: %f, price: %f, total_discount: %f, discount: %f\n",
        k, v.value_after_discount, v.value, v.price, v.total_discount, v.discount)
}

这将输出(在Go Playground 上尝试):

key: {Cash 1024}, sums: value_after_discount: 1024.000000, value: 1024.000000, price: 1.000000, total_discount: 0.000000, discount: 0.000000
key: {Service Revenue 1024}, sums: value_after_discount: 0.000000, value: 2048.000000, price: 2.000000, total_discount: 1024.000000, discount: 1.000000

再次重申:这是因为我们使用的输入数据包含相同的浮点常量字面量(这导致相同的float64 值)。实际上,由于 IEEE 754 浮点怪癖,分组可能会给出不正确的结果。如果可能,我建议使用int 作为数量。如果不可能,则将数量格式化为相同的格式(包括某些数字四舍五入)。

【讨论】:

    猜你喜欢
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    相关资源
    最近更新 更多