有没有提供关联数组功能的go库?
我正在寻找类似于python中“字典”的go语言功能,以方便一些python代码的转换。
编辑:地图对于这个重复数据删除应用程序来说效果很好。我能够使用具有 16 字节字符串索引的映射在短短几秒钟内将 1.3e6 个重复项压缩为 2.5e5 个唯一项。与地图相关的代码很简单,所以我将其包含在下面。值得注意的是,预先分配具有 1.3e6 元素的地图仅将速度提高了几个百分点:
var m = make(map[string]int, 1300000) // map with initial space for 1.3e6 elements
ct, ok := m[ax_hash]
if ok {
m[ax_hash] = ct + 1
} else {
m[ax_hash] = 1
}
I'm looking for a go language capability similar to the "dictionary" in python to facilitate the conversion of some python code.
EDIT: Maps worked quite well for this de-dupe application. I was able to condense 1.3e6 duplicated items down to 2.5e5 unique items using a map with a 16 byte string index in just a few seconds. The map-related code was simple so I've included it below. Worth noting that pre-allocation of map with 1.3e6 elements sped it up by only a few percent:
var m = make(map[string]int, 1300000) // map with initial space for 1.3e6 elements
ct, ok := m[ax_hash]
if ok {
m[ax_hash] = ct + 1
} else {
m[ax_hash] = 1
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对已经给出的答案进行一些扩展:
Go 映射是一种类型化的哈希映射数据结构。映射的类型签名的形式为
map[keyType]valueType
,其中keyType
和valueType
分别是键和值的类型。要初始化映射,必须使用
make
函数:未初始化的映射等于
nil
,如果读取或写入,则会在运行时发生恐慌。存储值的语法与数组或切片的语法非常相似:
同样,从映射中检索值的操作如下:
您可以使用
range
关键字通过range
迭代映射 。 code>for 循环:此代码将打印:
检索不在映射中的键的值将返回值类型的零值:
通过从映射中读取多个值,您可以测试键是否存在。第二个值将是一个布尔值,指示键的存在:
要从映射中删除键/值条目,您可以翻转它并将
false
指定为第二个值:您可以在映射中存储任意类型使用空接口类型
interface{}
:唯一的限制是,在检索这些值时,您必须执行类型断言才能以原始形式使用它们:
您可以使用类型开关来确定类型您要提取的值,并适当地处理它们:
在每个
case
块内,u
将是case< 中指定的类型/代码> 声明;不需要显式类型断言。
此代码将打印:
键可以是定义了相等运算符的任何类型,例如整数、浮点数、字符串和指针。也可以使用接口类型,只要底层类型支持相等性即可。 (结构体、数组和切片不能用作映射键,因为这些类型上没有定义相等性。)
,映射
o
可以采用上述任何类型的键:例如 简而言之。
To expand a little on answers already given:
A Go map is a typed hash map data structure. A map's type signature is of the form
map[keyType]valueType
wherekeyType
andvalueType
are the types of the keys and values respectively.To initialize a map, you must use the
make
function:An uninitialized map is equal to
nil
, and if read from or written a panic will occur at runtime.The syntax for storing values is much the same as doing so with arrays or slices:
Similarly, retrieving values from a map is done like so:
You can use the
range
keyword to iterate over a map with afor
loop:This code will print:
Retrieving a value for a key that is not in the map will return the value type's zero value:
By reading multiple values from a map, you can test for a key's presence. The second value will be a boolean indicating the key's presence:
To remove a key/value entry from a map, you flip it around and assign
false
as the second value:You can store arbitrary types in a map by using the empty interface type
interface{}
:The only proviso is that when retrieving those values you must perform a type assertion to use them in their original form:
You can use a type switch to determine the types of the values you're pulling out, and deal with them appropriately:
Inside each of those
case
blocks,u
will be of the type specified in thecase
statement; no explicit type assertion is necessary.This code will print:
The key can be of any type for which the equality operator is defined, such as integers, floats, strings, and pointers. Interface types can also be used, as long as the underlying type supports equality. (Structs, arrays and slices cannot be used as map keys, because equality is not defined on those types.)
For example, the map
o
can take keys of any of the above types:And that's maps in a nutshell.
地图类型。 http://golang.org/doc/ effective_go.html#maps
有一些区别来自 python 的原因是必须键入键,
所以你不能混合数字和字符串键(由于某种原因我忘记了你可以),但它们非常容易使用。The map type. http://golang.org/doc/effective_go.html#maps
There is some difference from python in that the keys have to be typed,
so you can't mix numeric and string keys(for some reason I forgot you can), but they're pretty easy to use.您可能正在寻找地图。
You're probably looking for a map.