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

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

Go lang slice columns from 2d array?

问题

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

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

/* 先尝试让行和列正常工作 */

func() isWin() bool {
  win := make([]char, SIZE*2)
  for i:= range BOARD {
    fmt.Println("行")
    win[i] = check(BOARD[i][0:SIZE])
    fmt.Println("列")
    win[i+SIZE] = check(BOARD[0:SIZE][i])
  }
return false
}

func() check(slice []char) (char) {
  fmt.Println(slice)
  return "-"
}

我给它以下输入:

[E E E E]
[E E E E]
[X O E E]
[X O E E]

但我得到的返回值是:

行
[X O E E]
列
[X O E E]

但我想要的返回值是:

行
[X O E E]
列
[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.

/* Just trying to get rows and columns working first */

func() isWin() bool {
  win := make([]char, SIZE*2)
  for i:= range BOARD {
    fmt.Println("Row")
    win[i] = check(BOARD[i][0:SIZE])
    fmt.Println("Column")
    win[i+SIZE] = check(BOARD[0:SIZE][i])
  }
return false
}

func() check(slice []char) (char) {
  fmt.Println(slice)
  return "-"
}

I give it the following input:

[E E E E]
[E E E E]
[X O E E]
[X O E E]

And I get a return of

Row
[X O E E]
Column
[X O E E]

But I want a return of

Row
[X O E E]
Column
[E E X X]

How do I make this slice?

答案1

得分: 8

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

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

func boardColumn(board [][]char, columnIndex int) (column []char) {
    column = make([]char, 0)
    for _, row := range board {
        column = append(column, row[columnIndex])
    }
    return
}

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

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

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

x := [][]int{{1,2,3},{4,5,6}}
fmt.Println(x[0:2]) // [[1,2,3],[4,5,6]]
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:

func boardColumn(board [][]char, columnIndex int) (column []char) {
    column = make([]char, 0)
    for _, row := range board {
        column = append(column, row[columnIndex])
    }
    return
}

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

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:

x := [][]int{{1,2,3},{4,5,6}}
fmt.Println(x[0:2]) // [[1,2,3],[4,5,6]]
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:

确定