GoLang:如何从二维切片中删除一个元素?

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

GoLang: How to delete an element from a 2D slice?

问题

最近我一直在尝试使用Go语言,并且想知道如何从一个二维切片中删除元素。

对于从一维切片中删除元素,我可以成功地使用以下代码:

data = append(data[:i], data[i+1:]...)

然而,对于二维切片,使用以下代码:

data = append(data[i][:j], data[i][j+1:]...)

会报错:

cannot use append(data[i][:j], data[i][j+1:]...) (type []string) as type [][]string in assignment

解决这个问题需要采用不同的方法吗?

英文:

I've recently been messing around with Go and I wanted to see how it would be to delete an element from a two-dimensional slice.

For deleting an element from a one-dimensional slice, I can successfully use:

data = append(data[:i], data[i+1:]...)

However, with a two-dimensional slice, using:

data = append(data[i][:j], data[i][j+1:]...)

throws the error:

cannot use append(data[i][:j], data[i][j+1:]...) (type []string) as type [][]string in assignment

Would tackling this require a different approach?

答案1

得分: 3

在Go语言中,二维切片实际上就是切片的切片。因此,如果你想从这个二维切片中删除一个元素,实际上你只需要从一个切片中删除一个元素(这个切片是另一个切片的元素)。

没有其他更复杂的操作。唯一需要注意的是,当你从行切片中删除一个元素时,结果只会是“外部”切片的行(一个元素)的“新”值,而不是二维切片本身。因此,你需要将结果赋给外部切片的一个元素,即刚刚删除的元素所在的行:

// 删除第i行第j列的元素:
s[i] = append(s[i][:j], s[i][j+1:]...)

请注意,如果我们用a替换s[i](这并不奇怪,因为s[i]表示我们要删除的“行切片”的第j个元素),这与简单的“从切片中删除”是相同的:

a = append(a[:j], a[j+1:]...)

请参考以下完整示例:

s := [][]int{
    {0, 1, 2, 3},
    {4, 5, 6, 7},
    {8, 9, 10, 11},
}

fmt.Println(s)

// 删除元素s[1][2](即6)
i, j := 1, 2
s[i] = append(s[i][:j], s[i][j+1:]...)

fmt.Println(s)

输出结果(在Go Playground上尝试):

[[0 1 2 3] [4 5 6 7] [8 9 10 11]]
[[0 1 2 3] [4 5 7] [8 9 10 11]]
英文:

A 2D slice in Go is nothing more than a slice of slices. So if you want to remove an element from this 2D slice, effectively you still only have to remove an element from a slice (which is an element of another slice).

There is nothing more involved. Only thing you have to look out is that when you remove an element from the row-slice, the result will only be the "new" value of the row (an element) of the "outer" slice, and not the 2D slice itself. So you have to assign the result to an element of the outer slice, to the row whose element you just removed:

// Remove element at the ith row and jth column:
s[i] = append(s[i][:j], s[i][j+1:]...)

Note that this is identical to the simple "removal from slice" if we substitute s[i] with a (not surprisingly, because s[i] denotes the "row-slice" whose jth element we're removing):

a = append(a[:j], a[j+1:]...)

See this complete example:

s := [][]int{
	{0, 1, 2, 3},
	{4, 5, 6, 7},
	{8, 9, 10, 11},
}

fmt.Println(s)

// Delete element s[1][2] (which is 6)
i, j := 1, 2
s[i] = append(s[i][:j], s[i][j+1:]...)

fmt.Println(s)

Output (try it on the Go Playground):

[[0 1 2 3] [4 5 6 7] [8 9 10 11]]
[[0 1 2 3] [4 5 7] [8 9 10 11]]

答案2

得分: 1

以下是可能的方法之一:

b := [][]int{
	[]int{1, 2, 3, 4},
	[]int{5, 6, 7, 8},
	[]int{9, 0, -1, -2},
	[]int{-3, -4, -5, -6},
}
print2D(b)
i, j := 2, 2

tmp := append(b[i][:j], b[i][j+1:]...)
c := append(b[:i], tmp)
c = append(c, b[i+1:]...)
print2D(c)

基本上,我提取了第i行,从中删除元素append(b[i][:j], b[i][j+1:]...),然后将此行放在其他行之间。

如果有人告诉我如何添加多个元素,代码看起来会更好看。

英文:

Here is one of the possible approaches <kbd>Go Playground</kbd>.

b := [][]int{
	[]int{1, 2, 3, 4},
	[]int{5, 6, 7, 8},
	[]int{9, 0, -1, -2},
	[]int{-3, -4, -5, -6},
}
print2D(b)
i, j := 2, 2


tmp := append(b[i][:j], b[i][j+1:]...)
c := append(b[:i], tmp)
c = append(c, b[i+1:]...)
print2D(c)

Basically I am extracting the i-th row, remove the element from it append(b[i][:j], b[i][j+1:]...) and then put this row between the rows.

If someone would tell how to append many elements, it would look even nicer.

huangapple
  • 本文由 发表于 2015年12月5日 16:04:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/34102704.html
匿名

发表评论

匿名网友

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

确定