英文:
Finding the length of 2D slices in go
问题
我想检查矩阵是否具有相同的大小:即两个矩阵是否具有相同的行数和列数。
矩阵1 := [][]int{{1,2,3} ,{4,5,6}}
矩阵2 := [][]int{{7,8,9}, {10,11,12}}
我得到 len(matrix1) == len(matrix2) == 2
。这是正确的行数。
如果我按照上面的方式声明矩阵,我如何检查每行的长度(即列数,应该是3)?
英文:
I want to check if the matrices are of the same size: if both matrices have the same number of rows and the same number of columns.
matrix1 := [][]int{{1,2,3} ,{4,5,6}}
matrix2 := [][]int{{7,8,9}, {10,11,12}}
I get len(matrix1) == len(matrix2) == 2
. Which is the correct number of rows.
How can I check the length of each row (i.e. the number of columns, which should be 3) if I'm declaring the matrices as shown above?
答案1
得分: 4
请注意,由于二维切片中的每一行长度可能是任意的,您应该检查每个对应行(具有相同索引)的长度是否相等。
以下是一个执行此操作的函数:
func match(m1, m2 [][]int) bool {
if len(m1) != len(m2) {
return false
}
for i, row1 := range m1 {
row2 := m2[i]
if len(row1) != len(row2) {
return false
}
}
return true
}
使用示例:
m1 := [][]int{{1, 2, 3}, {4, 5, 6}}
m2 := [][]int{{7, 8, 9}, {10, 11, 12}}
fmt.Println(match(m1, m2))
m1 = [][]int{{1, 2, 3}, {4, 5, 6, 7, 8}}
m2 = [][]int{{7, 8, 9}, {10, 11, 12, 12, 13}}
fmt.Println(match(m1, m2))
m1 = [][]int{{1, 2, 3}, {4, 5, 6, 7, 8}}
m2 = [][]int{{7, 8, 9}, {10, 11, 12, 12}}
fmt.Println(match(m1, m2))
m1 = [][]int{{1, 2, 3}}
m2 = [][]int{{7, 8, 9}, {10, 11, 12, 12}}
fmt.Println(match(m1, m2))
输出结果(如预期):
true
true
false
false
特殊情况的简化:
如果您保证在所有矩阵中,所有行的长度都相等,那么可以进行一种简化:在这种情况下,如果行数匹配,只需要比较每个矩阵的一个行(通常是第一行)的长度即可。
可以这样实现:
func match2(m1, m2 [][]int) bool {
if len(m1) != len(m2) {
return false
}
return len(m1) == 0 || len(m1[0]) == len(m2[0])
}
进行测试:
m1 = [][]int{{1, 2, 3}, {4, 5, 6}}
m2 = [][]int{{7, 8, 9}, {10, 11, 12}}
fmt.Println(match2(m1, m2))
m1 = [][]int{{1, 2, 3, 4}, {5, 6, 7, 8}}
m2 = [][]int{{7, 8, 9}, {10, 11, 12}}
fmt.Println(match2(m1, m2))
输出结果:
true
false
您可以在Go Playground上尝试这些代码。
英文:
Note that since every "row" in a 2D slice may have arbitrary length, you should check the length of each of the corresponding rows (having the same index) if they are equal.
Here's a function that does that:
func match(m1, m2 [][]int) bool {
if len(m1) != len(m2) {
return false
}
for i, row1 := range m1 {
row2 := m2[i]
if len(row1) != len(row2) {
return false
}
}
return true
}
See usage examples:
m1 := [][]int{{1, 2, 3}, {4, 5, 6}}
m2 := [][]int{{7, 8, 9}, {10, 11, 12}}
fmt.Println(match(m1, m2))
m1 = [][]int{{1, 2, 3}, {4, 5, 6, 7, 8}}
m2 = [][]int{{7, 8, 9}, {10, 11, 12, 12, 13}}
fmt.Println(match(m1, m2))
m1 = [][]int{{1, 2, 3}, {4, 5, 6, 7, 8}}
m2 = [][]int{{7, 8, 9}, {10, 11, 12, 12}}
fmt.Println(match(m1, m2))
m1 = [][]int{{1, 2, 3}}
m2 = [][]int{{7, 8, 9}, {10, 11, 12, 12}}
fmt.Println(match(m1, m2))
Output (as expected):
true
true
false
false
Simplification for Special case:
If you have guarantee that in all of your matrices all rows have the same length, you can make a big simplification: in this case if number of rows matches, it's enough to compare the length of one of the rows only from each matrices, practically the first row.
It could look like this:
func match2(m1, m2 [][]int) bool {
if len(m1) != len(m2) {
return false
}
return len(m1) == 0 || len(m1[0]) == len(m2[0])
}
Testing it:
m1 = [][]int{{1, 2, 3}, {4, 5, 6}}
m2 = [][]int{{7, 8, 9}, {10, 11, 12}}
fmt.Println(match2(m1, m2))
m1 = [][]int{{1, 2, 3, 4}, {5, 6, 7, 8}}
m2 = [][]int{{7, 8, 9}, {10, 11, 12}}
fmt.Println(match2(m1, m2))
Output:
true
false
Try these on the <kbd>Go Playground</kbd>.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论