slice

Extract submatrix at certain row/col values

拜拜、爱过 提交于 2021-02-16 19:03:07
问题 I need to slice a 2D input array from row/col indices and a slicing distance. In my example below, I can extract a 3x3 submatrix from an input matrix, but I cannot adapt this code to work for any search distance I would like, without resorting to manually writing down the indices: Example: import numpy as np # create matrix mat_A = np.arange(100).reshape((10, 10)) row = 5 col = 5 # Build 3x3 matrix around the centre point matrix_three = ((row - 1, col - 1), (row, col - 1), (row + 1, col - 1),

How to explain golang slice range's phenomenon [duplicate]

我们两清 提交于 2021-02-13 17:41:23
问题 This question already has answers here : Golang Reusing Memory Address Copying from slice? (2 answers) Closed 3 years ago . type student struct { Name string Age int } func main() { m := make(map[string]*student) s := []student{ {Name: "Allen", Age: 24}, {Name: "Tom", Age: 23}, } for _, stu := range s { m[stu.Name] = &stu } fmt.Println(m) for key, value := range m { fmt.Println(key, value) } } result: map[Allen:0xc42006a0c0 Tom:0xc42006a0c0] Allen &{Tom 23} Tom &{Tom 23} How to explain Slice

How to explain golang slice range's phenomenon [duplicate]

夙愿已清 提交于 2021-02-13 17:40:17
问题 This question already has answers here : Golang Reusing Memory Address Copying from slice? (2 answers) Closed 3 years ago . type student struct { Name string Age int } func main() { m := make(map[string]*student) s := []student{ {Name: "Allen", Age: 24}, {Name: "Tom", Age: 23}, } for _, stu := range s { m[stu.Name] = &stu } fmt.Println(m) for key, value := range m { fmt.Println(key, value) } } result: map[Allen:0xc42006a0c0 Tom:0xc42006a0c0] Allen &{Tom 23} Tom &{Tom 23} How to explain Slice

How to categorize list of objects based on a parameter

我们两清 提交于 2021-02-11 14:32:46
问题 I have a list of objects with this definition: type MyObject struct { ID int `json:"id"` Level int `json:"level"` CityID int `json:"city_id"` } I want to categorize them based on CityID in order to get a list of lists where each inner list's items have the same CityID . For example, if I have the following list: [ MyObject {ID: 1, Level: 12, CityID: 7}, MyObject {ID: 2, Level: 15, CityID: 2}, MyObject {ID: 3, Level: 55, CityID: 4}, MyObject {ID: 4, Level: 88, CityID: 7}, MyObject {ID: 5,

Equality (Identity) of Go Slices

三世轮回 提交于 2021-02-10 13:26:18
问题 My question is slightly different from this question that asks about how to check equality of Go slices. Like this article suggests, a Go slice is a value consisting of three things: a pointer to the array, the length of the segment, and its capacity (the maximum length of the segment). Is it then possible to (cheaply) check if two such slices are equal because they point to the same underlying array and have the same values for length and capacity (preferably without traversing the two

Why doesn't the python slice syntax wrap around from negative to positive indices?

青春壹個敷衍的年華 提交于 2021-02-08 13:55:45
问题 I noticed, given l = [1,2,3] , that l[-1:] returns [3] as expected, but that l[-1:0] returns [] , very much unlike what I expected. I then tried [-1:1] , which I expected to return [3,1] , but it also returns [] . Is there a good reason why the slice syntax does not wrap around from negative to positive indices (and the other way round)? It seems it would be pretty useful and pretty straightforward to implement, but maybe I'm missing something. 回答1: Slicing has a very simple definition: you

Method does not change the value of object if the object is in a slice

白昼怎懂夜的黑 提交于 2021-02-08 10:20:14
问题 Here is my program: package main import ( "fmt" ) type Number struct { val int } func (num * Number) Increment () { num.val += 1 } func (num Number) Value() int { return num.val } func main() { numbers := []Number { {val: 12}, {val: 7}, {val: 0}, } for _, each := range numbers { each.Increment() fmt.Println(each.Value()) } for _, each := range numbers { fmt.Println(each.Value()) } } Here is the output: 13 8 1 12 7 0 First question: why does the Increment() method not update the value in the

Method does not change the value of object if the object is in a slice

笑着哭i 提交于 2021-02-08 10:19:41
问题 Here is my program: package main import ( "fmt" ) type Number struct { val int } func (num * Number) Increment () { num.val += 1 } func (num Number) Value() int { return num.val } func main() { numbers := []Number { {val: 12}, {val: 7}, {val: 0}, } for _, each := range numbers { each.Increment() fmt.Println(each.Value()) } for _, each := range numbers { fmt.Println(each.Value()) } } Here is the output: 13 8 1 12 7 0 First question: why does the Increment() method not update the value in the

How to copy from slice to array with seeking onto slice

拟墨画扇 提交于 2021-02-08 03:44:25
问题 I'm writing a library to deal with a binary format. I have a struct with array vars, that I would like to keep for documentation purposes. I need also to seek and tell from the input slice of bytes. Some pseudocode: type foo struct { boo [5]byte coo [3]byte } func main() { // input is a []byte full of datas, read from a file var bar foo // Here i need something that writes 5 bytes to bar.foo from input bar.foo = somefunc(input, numberOfFoo) // ??? // I need also tell() and seek() input.seek(n

Slicing a list without copying

狂风中的少年 提交于 2021-02-07 19:18:52
问题 This is my first question on the forum and I'm also really new to Python, so maybe this doesn't make a lot of sense, but I'm just left wondering... It's very related, but IMO this question Slicing a list in Python without generating a copy does not answer the question of how to slice a list in such a fashion that changing the slice would change the original? Say, if I want to change only part of a list in a function and make sure that the function doesn't have access to all the members, how