为了账号安全,请及时绑定邮箱和手机立即绑定

如何在golang中按多个值分组并对多个值求和

如何在golang中按多个值分组并对多个值求和

Go
MYYA 2022-10-10 17:02:28
我有这个代码    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)我想用accountandquantity和value_after_discountwithvalue_after_discount和valuewithvalue和pricewithprice和total_discountwithtotal_discount和discountwith进行分组discount。并且输出应该是map[{Cash 1024}:[1024 1024 1 0 0] {Service Revenue 1024}:[1024 2048 2 1024 1]]https://play.golang.org/p/KKTmovpfN1z
查看完整描述

1 回答

?
烙印99

TA贡献1829条经验 获得超13个赞

使用结构作为account和quantity字段的组合键。注意:浮点数比较可能会让您感到惊讶,如果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数量。如果不可能,则将数量格式化为相同的格式(包括某些数字舍入)。


查看完整回答
反对 回复 2022-10-10
  • 1 回答
  • 0 关注
  • 175 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号