我正在努力寻找答案的一个基本问题,因为有很多关于如何使用 append 函数和错误使用“数组”一词的扩展运算符连接两个切片的答案。
我是 Go 的新手,并假设使用大小数组是已知大小的好习惯。但是,我正在努力处理数组,因为我无法弄清楚如何进行简单的操作,例如连接。这是一些代码。
var seven [7]int
five := [5]int{1,2,3,4,5}
two := [2]int{6,7}
//this doesn't work as both the inputs and assignment are the wrong type
seven = append(five,two)
//this doesn't work as the assignment is still the wrong type
seven = append(five[:],two[:])
//this works but I'm not using arrays anymore so may as well use slices everywhere and forget sizing
seven2 := append(five[:],two[:])
Run Code Online (Sandbox Code Playgroud)
据我所知,我可以放弃数组并专门使用切片,或者我可以编写一个循环来显式构造新数组。有第三种选择吗?
我有这个代码:
a = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype='uint8')
a-1
Run Code Online (Sandbox Code Playgroud)
为什么我得到:
array([255, 255, 255, 255, 255, 255, 255, 255, 255, 255], dtype=uint8)
Run Code Online (Sandbox Code Playgroud)
其结果?
并不是:
array([-1, -1, -1,...], dtype=uint8)
Run Code Online (Sandbox Code Playgroud)