我正在将一个程序从 python 转换为 golang,我有一行获取嵌套列表中的第一个值:
x_values = map(operator.itemgetter(0), self.coords)
此命令将 [[1,2],[2,3],[7,4]]
转换为 [1,2,7]
。
在 go 中有类似的东西吗?
最佳答案
Go 中的等价物是 for
循环:
package main
import (
"fmt"
)
func main() {
a := make([][]int, 3)
a[0] = []int{1, 2}
a[1] = []int{2, 3}
a[2] = []int{7, 4}
b := make([]int, len(a))
for i, v := range a {
if len(v) > 0 {
b[i] = v[0]
}
}
fmt.Println(b)
}
关于python - 相当于golang中的itemgetter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42740726/