How do I get any given element of a map in Go?

huangapple go评论84阅读模式
英文:

How do I get any given element of a map in Go?

问题

假设我有一个 map[string]int,我想要找到长度(以字节为单位)最短的键。如果我不知道地图中包含的任何特定元素,我该如何获取一个样本,以便我可以执行以下操作:

var shortest string
for key, _ := range myMap {
    if len(key) < len(shortest) {
        shortest = key
    }
}
英文:

Suppose I have a map[string]int, and I want the key with the shortest length (in bytes). If I don't know any particular element that the map contains, how do I get a sample of it so I can do

var shortest string
for key, _ := range myMap {
    if len(key) &lt; len(shortest) {
        shortest = key
    }
}

答案1

得分: 2

你可以使用for循环,并立即跳出循环来仅采样一个元素。

var shortest string
for key, _ := range myMap {
    shortest = key
    break
}
for key, _ := range myMap {
    if len(key) < len(shortest) {
        shortest = key
    }
}

虽然不太美观,但它可以工作。

英文:

You can use a for loop and break out of it immediately to sample just one element.

var shortest string
for key, _ := range myMap {
    shortest = key
    break
}
for key, _ := range myMap {
    if len(key) &lt; len(shortest) {
        shortest = key
    }
}

Ugly, but it works.

答案2

得分: 1

你应该首先定义两个变量shortestLengthshortest,它们将分别记录到目前为止找到的最短长度和对应的键。然后开始遍历地图。

在这里,技巧是用一个值初始化shortestLength变量,这个值将在第一次遍历中被覆盖。好处是你不需要编写任何额外的代码和分配额外的内存来对键进行排序和找到最短的键。

完整的代码如下:

if len(myMap) == 0 {
   // 空地图
}

// 将在第一次迭代中被覆盖
shortestLength := maths.MaxInt32
shortest := ""

for key, _ := range myMap {
  keyLength := len(key)
  if keyLength <= shortestLength {
    shortest = key
    shortestLength = keyLength
  }
}

变量shortestLength将在第一次迭代中被最短键的长度覆盖。在循环结束时,它将包含最短键的长度。而shortest将包含键本身。

英文:

You should first define two variables shortestLength and shortest, they will record shortest length you found until now, and corresponding key, respectively. And then start iterating over the map.

Here, the trick is to initialise shortestLength variable with a value, which will be over-written in the first pass. The benefit is that, you don't have to write any extra code and allocate extra memory to sort the keys, and find the shortest one.

Complete code is as following:

if len(myMap) == 0 {
   // Empty map
}

// Will be over-written in first iteration
shortestLength := maths.MaxInt32
shortest := &quot;&quot;

for key, _ := range myMap {
  keyLength := len(key)
  if keyLength &lt;= shortestLength {
    shortest = key
    shortestLength = keyLength
  }
}

The variable shortestLength will be over-written with the length of first element in the first iteration of our for loop. And at the end of the loop will contain the length of shortest key. And shortest will contain the key itself.

答案3

得分: 0

你可以遍历这个映射,将元素添加到一个切片中。然后对切片进行排序:

var keys []string
for k := range myMap {
    keys = append(keys, k)
}
sort.Strings(keys) // keys[0] 是最短的

GoPlay: http://play.golang.org/p/DULIiQAkfg

英文:

You could iterate over the map, adding the elements to a slice. Then sort the slice:

var keys []string
for k := range myMap {
	keys = append(keys, k)
}
sort.Strings(keys) // keys[0] is the shorted

GoPlay: http://play.golang.org/p/DULIiQAkfg

huangapple
  • 本文由 发表于 2016年4月14日 02:31:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/36606717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定