- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的目标是获取可能包含多达一百万个条目的 map[string]int
并将其分成最多 500 个大小的 block ,并将 map POST 到外部服务。我是 golang 新手,所以现在正在 Go Playground 中进行修改。
Any tips anyone has on how to improve the efficiency of my code base, please share!
Playground :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,
这里的问题是,虽然正确计算了 block 书挡,但 x
值每次都从 0 开始。我想我应该期望它从 block 书尾最小值开始,即 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)
}
}
最佳答案
当你处理一个 block 时:
for x := range keys[chunkStart:chunkEnd] {}
您正在迭代一个 slice ,并且有一个迭代变量,它将是 slice 索引,而不是 slice 中的元素(在给定索引处)。因此它总是从 0
开始。 (当您迭代映射时,第一个迭代变量是键,因为那里没有索引,第二个迭代变量是与该键关联的值。)
相反,你想要这个:
for _, key := range keys[chunkStart:chunkEnd] {}
另请注意,首先将键收集到 slice 中,然后再处理它们是多余的。首先,您可以在迭代 map 一次时执行此操作。只需保留一个对迭代进行计数的变量即可知道何时达到 block 大小,如果您使用保留该大小的数据结构(例如键批处理 slice 的大小),则这可能是隐式的。
例如(在 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()
}
关于dictionary - 如何将map[string]int切成 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57871411/
我正在用 Python (2.6) 编写一个应用程序,需要我使用字典作为数据存储。 我很好奇拥有一个大字典是否更节省内存,或者将其分解为许多(很多)较小的字典,然后拥有一个包含对所有较小字典的引用的“
Convert this [ "Cat" : ["A" : 1, "B": 2], "Mat" : ["C" : 3, "D": 4] ] Into [ "A" : 1,
有什么很酷的快速方法可以让两个字典创建第三个字典,以内连接方式将第一个字典的键映射到第二个字典的值? Dictionary dic1 = new Dictionary {{a1,b1},{a2,b2}
我希望将字典相互嵌套,以便容纳 block 的 xy 坐标。所以我会 IDictionary, IDictionary> 键 Dictionary 包含列、行组合,而值 Dictionary 包含 x
在 C# 中,我需要将数据保存在字典对象中,如下所示: Dictionary> MyDict = new Dictionary>(); 现在我意识到,在某些情况下我需要一些其他(不是字典类的)
第一个Dictionary就像 Dictionary ParentDict = new Dictionary(); ParentDict.Add("A_1", "1")
我似乎无法理解这个问题。我需要使用 LINQ 按内部字典的值对字典进行排序。有什么想法吗? 最佳答案 你的意思是你想要所有的值,按内部值排序? from outerPair in outer from
我想建模一个模式,其中响应是字典: { 'id1': { 'type': 'type1', 'active': true, }, 'id2': { 'type':
我有以下代码要添加或更新(如果已经存在)dict()-dict 中的值: if id not in self.steps: self.steps[ id ] = step else:
我有一个包含字典的 Swift 字典,我想使用存储的属性来访问键值: var json = [NSObject:AnyObject]() var title: String { get
我想创建一个 Dictionary>结构,我想提供一个 IEqualityComparer在包含 APerson 的second 字典中作为关键 如果我只有内部字典,那就是 var f = new D
我有一个集合,其中包含如下文档:文档 1: { "company": "ABC" "application": { "app-1": {"earning_from_src_A": 50,
我正在快速学习。 我发现 dictionary 就像 hash 用于 PHP 或其他一些语言。 那我怎么制作dictionary的dictionary呢?? 我有这样的数据 key:J name:jh
这个问题在这里已经有了答案: Explode a dict - Get all combinations of the values in a dictionary (2 个答案) 关闭 5 个月前
我是编程新手,所以如果我的问题看起来很愚蠢,我很抱歉。我想问一下有没有办法从 Multi.Dictionary 返回key当我有值(value)? 这是我的代码: Dim myDict Set myD
我试图找出标准 Ada 库是否配备了“字典”类型(我的意思是:一种以 格式存储值的数据结构,我可以从中检索 value 使用相应的唯一 key)。 这样的数据结构存在吗?如果是这样,有人可以提供一个
我究竟做错了什么?根据我的测试,objDic.exists 永远不会给出 False! dim objDic set objDic = createobject("scripting.
我想创建一个复合类型,其中包含一个字典作为其命名字段之一。但是明显的语法不起作用。我敢肯定有一些我不明白的基本原理。下面是一个例子: type myType x::Dict() end Jul
julia> hotcell2vocab = Dict([(cell, i-1+vocab_start) for (i,cell) in enumerate(h
我有一个简单的问题:我对 Dictionary.Value 集合进行了很多次迭代,这让我很烦,我必须调用 .ToList() 然后才能调用 .ForEach(),因为它似乎没有可枚举的Dictiona
我是一名优秀的程序员,十分优秀!