有没有提供关联数组功能的go库?

发布于 2024-09-15 03:34:31 字数 395 浏览 11 评论 0原文

我正在寻找类似于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恏ㄋ傷疤忘ㄋ疼 2024-09-22 03:34:31

对已经给出的答案进行一些扩展:

Go 映射是一种类型化的哈希映射数据结构。映射的类型签名的形式为 map[keyType]valueType,其中 keyTypevalueType 分别是键和值的类型。

要初始化映射,必须使用make函数:

m := make(map[string]int)

未初始化的映射等于nil,如果读取或写入,则会在运行时发生恐慌。

存储值的语法与数组或切片的语法非常相似:

m["Alice"] = 21
m["Bob"] = 17

同样,从映射中检索值的操作如下:

a := m["Alice"]
b := m["Bob"]

您可以使用 range 关键字通过 range 迭代映射 。 code>for 循环:

for k, v := range m {
    fmt.Println(k, v)
}

此代码将打印:

Alice 21
Bob 17

检索不在映射中的键的值将返回值类型的零值:

c := m["Charlie"]
// c == 0

通过从映射中读取多个值,您可以测试键是否存在。第二个值将是一个布尔值,指示键的存在:

a, ok := m["Alice"]
// a == 21, ok == true
c, ok := m["Charlie"]
// c == 0, ok == false

要从映射中删除键/值条目,您可以翻转它并将 false 指定为第二个值:

m["Bob"] = 0, false
b, ok := m["Bob"]
// b == 0, ok == false

您可以在映射中存储任意类型使用空接口类型 interface{}

n := make(map[string]interface{})
n["One"] = 1
n["Two"] = "Two"

唯一的限制是,在检索这些值时,您必须执行类型断言才能以原始形式使用它们:

a := n["One"].(int)
b := n["Two"].(string)

您可以使用类型开关来确定类型您要提取的值,并适当地处理它们:

for k, v := range n {
    switch u := v.(type) {
        case int:
            fmt.Printf("Key %q is an int with the value %v.\n", k, u)
        case string:
            fmt.Printf("Key %q is a string with the value %q.\n", k, u)
    }
}

在每个 case 块内,u 将是 case< 中指定的类型/代码> 声明;不需要显式类型断言。

此代码将打印:

Key "One" is an int with the value 1.
Key "Two" is a string with the value "Two".

键可以是定义了相等运算符的任何类型,例如整数、浮点数、字符串和指针。也可以使用接口类型,只要底层类型支持相等性即可。 (结构体、数组和切片不能用作映射键,因为这些类型上没有定义相等性。)

,映射 o 可以采用上述任何类型的键:

o := make(map[interface{}]int)
o[1] = 1
o["Two"] = 2

例如 简而言之。

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 where keyType and valueType are the types of the keys and values respectively.

To initialize a map, you must use the make function:

m := make(map[string]int)

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:

m["Alice"] = 21
m["Bob"] = 17

Similarly, retrieving values from a map is done like so:

a := m["Alice"]
b := m["Bob"]

You can use the range keyword to iterate over a map with a for loop:

for k, v := range m {
    fmt.Println(k, v)
}

This code will print:

Alice 21
Bob 17

Retrieving a value for a key that is not in the map will return the value type's zero value:

c := m["Charlie"]
// c == 0

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:

a, ok := m["Alice"]
// a == 21, ok == true
c, ok := m["Charlie"]
// c == 0, ok == false

To remove a key/value entry from a map, you flip it around and assign false as the second value:

m["Bob"] = 0, false
b, ok := m["Bob"]
// b == 0, ok == false

You can store arbitrary types in a map by using the empty interface type interface{}:

n := make(map[string]interface{})
n["One"] = 1
n["Two"] = "Two"

The only proviso is that when retrieving those values you must perform a type assertion to use them in their original form:

a := n["One"].(int)
b := n["Two"].(string)

You can use a type switch to determine the types of the values you're pulling out, and deal with them appropriately:

for k, v := range n {
    switch u := v.(type) {
        case int:
            fmt.Printf("Key %q is an int with the value %v.\n", k, u)
        case string:
            fmt.Printf("Key %q is a string with the value %q.\n", k, u)
    }
}

Inside each of those case blocks, u will be of the type specified in the case statement; no explicit type assertion is necessary.

This code will print:

Key "One" is an int with the value 1.
Key "Two" is a string with the value "Two".

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:

o := make(map[interface{}]int)
o[1] = 1
o["Two"] = 2

And that's maps in a nutshell.

丘比特射中我 2024-09-22 03:34:31

地图类型。 http://golang.org/doc/ effective_go.html#maps

有一些区别来自 python 的原因是必须键入键,所以你不能混合数字和字符串键(由于某种原因我忘记了你可以),但它们非常容易使用。

dict := make(map[string]string)
dict["user"] = "so_user"
dict["pass"] = "l33t_pass1"

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.

dict := make(map[string]string)
dict["user"] = "so_user"
dict["pass"] = "l33t_pass1"
吃兔兔 2024-09-22 03:34:31

您可能正在寻找地图

You're probably looking for a map.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文