Go语言从二维数组中切片列?

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

Go lang slice columns from 2d array?

问题

我很好奇,你如何从一个二维数组中创建一个列切片?

我有一个用于井字游戏棋盘的数组,我试图创建一个列切片,但我的切片都是相同的。

  1. /* 先尝试让行和列正常工作 */
  2. func() isWin() bool {
  3. win := make([]char, SIZE*2)
  4. for i:= range BOARD {
  5. fmt.Println("行")
  6. win[i] = check(BOARD[i][0:SIZE])
  7. fmt.Println("列")
  8. win[i+SIZE] = check(BOARD[0:SIZE][i])
  9. }
  10. return false
  11. }
  12. func() check(slice []char) (char) {
  13. fmt.Println(slice)
  14. return "-"
  15. }

我给它以下输入:

  1. [E E E E]
  2. [E E E E]
  3. [X O E E]
  4. [X O E E]

但我得到的返回值是:

  1. [X O E E]
  2. [X O E E]

但我想要的返回值是:

  1. [X O E E]
  2. [E E X X]

我该如何创建这个切片?

英文:

I'm curious, how do you create a column slice from a 2d Array?

I have an array for a game board for Tic-Tac-Toe, and I'm trying to create a column slice, but my slices are coming out identical.

  1. /* Just trying to get rows and columns working first */
  2. func() isWin() bool {
  3. win := make([]char, SIZE*2)
  4. for i:= range BOARD {
  5. fmt.Println("Row")
  6. win[i] = check(BOARD[i][0:SIZE])
  7. fmt.Println("Column")
  8. win[i+SIZE] = check(BOARD[0:SIZE][i])
  9. }
  10. return false
  11. }
  12. func() check(slice []char) (char) {
  13. fmt.Println(slice)
  14. return "-"
  15. }

I give it the following input:

  1. [E E E E]
  2. [E E E E]
  3. [X O E E]
  4. [X O E E]

And I get a return of

  1. Row
  2. [X O E E]
  3. Column
  4. [X O E E]

But I want a return of

  1. Row
  2. [X O E E]
  3. Column
  4. [E E X X]

How do I make this slice?

答案1

得分: 8

你想要用切片语法实现的功能是不可能的。你认为x[i][0:n]会获取到第i行的所有列。实际上,它返回的是第i行的第0n列。

你需要使用循环来获取一列:

  1. func boardColumn(board [][]char, columnIndex int) (column []char) {
  2. column = make([]char, 0)
  3. for _, row := range board {
  4. column = append(column, row[columnIndex])
  5. }
  6. return
  7. }

你标记为“Row”的代码实际上是我期望的获取列的代码:

  1. win[i] = check(BOARD[0:SIZE][i])

从语义上讲,切片的索引表示:从零到SIZE,取所有元素的第i个元素。
但是,正如前面提到的,Go的切片工作方式不同。自己试试

  1. x := [][]int{{1,2,3},{4,5,6}}
  2. fmt.Println(x[0:2]) // [[1,2,3],[4,5,6]]
  3. fmt.Println(x[0:2][0]) // [1,2,3]

如你在上面的示例的第二行中所看到的,x[0:2]返回整个二维切片。因此,取第一个元素返回的是该二维切片的第一行。

英文:

What you want to do is not possible with the slice syntax. You think that x[i][0:n] will get you all the columns of row i. What it actually does is returning the columns 0 to n from row i.

You have to use a loop to get a column:

  1. func boardColumn(board [][]char, columnIndex int) (column []char) {
  2. column = make([]char, 0)
  3. for _, row := range board {
  4. column = append(column, row[columnIndex])
  5. }
  6. return
  7. }

The code you label with 'Row' is actually the code I would have expected to deliver the columns:

  1. win[i] = check(BOARD[0:SIZE][i])

Semantically the slice indices say: take the ith element of all elements from zero to SIZE.
But, as already mentioned, Go slices work differently. Try for yourself:

  1. x := [][]int{{1,2,3},{4,5,6}}
  2. fmt.Println(x[0:2]) // [[1,2,3],[4,5,6]]
  3. fmt.Println(x[0:2][0]) // [1,2,3]

As you can see in line two of the example above, x[0:2] returns the full 2d slice. Consequently
taking the first element returns the first row of that 2d slice.

huangapple
  • 本文由 发表于 2013年3月25日 23:02:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/15618155.html
匿名

发表评论

匿名网友

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

确定