登录
首页 >  Golang >  Go问答

将mapint切成分段

来源:stackoverflow

时间:2024-02-24 18:30:26 308浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《将mapint切成分段》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

我的目标是获取可能包含多达一百万个条目的 map[string]int 并将其分成最多 500 个大小的块,并将映射 post 到外部服务。我是 golang 新手,所以现在正在修改 go 演示。

如果有人对如何提高我的代码库效率有任何建议,请分享!

演示:https://play.golang.org/p/ej4_pd9x91c

我看到的 cli 输出是:

original size 60
chunk bookends 0 20
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
chunk bookends 20 40
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
chunk bookends 40 60
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,

这里的问题是,虽然正确计算了块书挡,但 x 值每次都从 0 开始。我想我应该期望它从块书尾最小值开始,这将是 0、20、40 等。为什么范围每次都从零开始?

来源:

package main

import (
    "fmt"
    "math/rand"
    "strconv"
)

func main() {
    items := make(map[string]int)

    // Generate some fake data for our testing, in reality this could be 1m entries
    for i := 0; i < 60; i ++ {
        // int as strings are intentional here
        items[strconv.FormatInt(int64(rand.Int()), 10)] = rand.Int()
    }

    // Create a map of just keys so we can easily chunk based on the numeric keys
    i := 0
    keys := make([]string, len(items))
    for k := range items {
            keys[i] = k
            i++
    }

    fmt.Println("original size", len(keys))
    //batchContents := make(map[string]int)

    // Iterate numbers in the size batch we're looking for
    chunkSize := 20
    for chunkStart := 0; chunkStart < len(keys); chunkStart += chunkSize {
        chunkEnd := chunkStart + chunkSize

        if chunkEnd > len(items) {  
            chunkEnd = len(items)
        }

        // Iterate over the keys
        fmt.Println("chunk bookends", chunkStart, chunkEnd)
        for x := range keys[chunkStart:chunkEnd] {
            fmt.Print(x, ",")

            // Build the batch contents with the contents needed from items
            // @todo is there a more efficient approach?
            //batchContents[keys[i]] = items[keys[i]]
        }
        fmt.Println()

        // @todo POST final batch contents
        //fmt.Println(batchContents)
    }

}

解决方案


当你处理一个块时:

for x := range keys[chunkstart:chunkend] {}

您正在迭代一个切片,并且有一个迭代变量,它将是切片索引,而不是切片中的元素(在给定索引处)。因此它总是从 0 开始。 (当您迭代映射时,第一个迭代变量是键,因为那里没有索引,第二个迭代变量是与该键关联的值。)

相反,你想要这个:

for _, key := range keys[chunkstart:chunkend] {}

另请注意,首先将键收集到切片中,然后再处理它们是多余的。首先,您可以在迭代地图一次时执行此操作。只需保留一个对迭代进行计数的变量即可知道何时达到块大小,如果您使用保留该大小的数据结构(例如键批处理切片的大小),则这可能是隐式的。

例如(在 Go Playground 上尝试一下):

chunkSize := 20
batchKeys := make([]string, 0, chunkSize)
process := func() {
    fmt.Println("Batch keys:", batchKeys)
    batchKeys = batchKeys[:0]
}

for k := range items {
    batchKeys = append(batchKeys, k)
    if len(batchKeys) == chunkSize {
        process()
    }
}
// Process last, potentially incomplete batch
if len(batchKeys) > 0 {
    process()
}

好了,本文到此结束,带大家了解了《将mapint切成分段》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>