slice

Can't use range on slice made with reflect then passed json.Unmarshal

て烟熏妆下的殇ゞ 提交于 2020-07-08 02:31:17
问题 I am getting the following errors from the code below: invalid indirect of typedSlice (type interface {}) cannot range over typedSlice (type interface {}) This is confusing to me because reflect.TypeOf(copy) matches the type of t . func Unmarshal(t reflect.Type) []interface{} { ret := []interface{}{} s := `[{"Name":"The quick..."}]` slice := reflect.Zero(reflect.SliceOf(t)) o := reflect.New(slice.Type()) o.Elem().Set(slice) typedSlice := o.Interface() json.Unmarshal([]byte(s), typedSlice) fmt

How to slice a struct array?

泄露秘密 提交于 2020-06-27 07:26:28
问题 How can I extract a specific field from each element of a Matlab struct array? >> clear x >> x(1).a = 6; >> x(2).a = 7; I'd like an array containing 6 and 7. Neither x(:).a nor x.a do what I want. >> x(:).a ans = 6 ans = 7 回答1: No problem - just use : arr = [x.a]; It will concat all of the values that you need. If you have a more complex data, you can use the curly bracers: b(1).x = 'John'; b(2).x = 'Doe'; arr = {b.x}; 回答2: For a multi-dimensional array, you need reshape([x.a], size(x)) 回答3:

How to slice a struct array?

扶醉桌前 提交于 2020-06-27 07:26:09
问题 How can I extract a specific field from each element of a Matlab struct array? >> clear x >> x(1).a = 6; >> x(2).a = 7; I'd like an array containing 6 and 7. Neither x(:).a nor x.a do what I want. >> x(:).a ans = 6 ans = 7 回答1: No problem - just use : arr = [x.a]; It will concat all of the values that you need. If you have a more complex data, you can use the curly bracers: b(1).x = 'John'; b(2).x = 'Doe'; arr = {b.x}; 回答2: For a multi-dimensional array, you need reshape([x.a], size(x)) 回答3:

Is it possible to initialize slice with specific values?

不问归期 提交于 2020-06-24 20:21:22
问题 Is it possible to initialize an slice with all 1's like in python? PYTHON: onesArray = np.ones(5) onesList = [1]*5 GOLANG onesSlice := make([]int, 5) for i:= 0; i < len(onesSlice); i++{ onesSlice[i] = 1 } Is it possible to do better than this? 回答1: Yes but you have to use a different syntax. oneSlice := []int{1, 1, 1, 1, 1} It's referred to as 'composite literal' Also, if there is reason to iterate (like calculating the values based loop variable or something) then you could use the range

Why go doesn't report “slice bounds out of range” in this case? [duplicate]

旧时模样 提交于 2020-06-23 18:50:26
问题 This question already has answers here : Why does go allow slicing from len(slice)? (3 answers) Closed 2 years ago . Here's the code to reproduce: package main import "fmt" func main() { var v []int v = append(v,1) v = append(v,v[1:]...) fmt.Println("hi", v) } v[1] will report index out of range , but v[1:]... won't, why? 回答1: That's how spec defines slice expressions For arrays or strings, the indices are in range if 0 <= low <= high <= len(a), otherwise they are out of range. For slices,

Why go doesn't report “slice bounds out of range” in this case? [duplicate]

这一生的挚爱 提交于 2020-06-23 18:48:31
问题 This question already has answers here : Why does go allow slicing from len(slice)? (3 answers) Closed 2 years ago . Here's the code to reproduce: package main import "fmt" func main() { var v []int v = append(v,1) v = append(v,v[1:]...) fmt.Println("hi", v) } v[1] will report index out of range , but v[1:]... won't, why? 回答1: That's how spec defines slice expressions For arrays or strings, the indices are in range if 0 <= low <= high <= len(a), otherwise they are out of range. For slices,

How to remove all element from array except the first one in javascript

橙三吉。 提交于 2020-06-22 09:37:45
问题 I want to remove all element from array except the element of array at 0th index ["a", "b", "c", "d", "e", "f"] Output should be a 回答1: You can set the length property of the array. var input = ['a','b','c','d','e','f']; input.length = 1; console.log(input); OR, Use splice(startIndex) method var input = ['a','b','c','d','e','f']; input.splice(1); console.log(input); OR use Array.slice method var input = ['a','b','c','d','e','f']; var output = input.slice(0, 1) // 0-startIndex, 1 - endIndex

How to remove all element from array except the first one in javascript

守給你的承諾、 提交于 2020-06-22 09:36:27
问题 I want to remove all element from array except the element of array at 0th index ["a", "b", "c", "d", "e", "f"] Output should be a 回答1: You can set the length property of the array. var input = ['a','b','c','d','e','f']; input.length = 1; console.log(input); OR, Use splice(startIndex) method var input = ['a','b','c','d','e','f']; input.splice(1); console.log(input); OR use Array.slice method var input = ['a','b','c','d','e','f']; var output = input.slice(0, 1) // 0-startIndex, 1 - endIndex

How do I convert string of int slice to int slice?

你说的曾经没有我的故事 提交于 2020-06-09 07:08:49
问题 Example input: (type: string ) "[156, 100, 713]" Example conversion: (type: slice of int ) [156, 100, 713] 回答1: In addition to mhutter's answer, also note that your input string looks like a JSON array (maybe it is from a JSON text?). If you treat it like that, you may unmarshal its content into an []int slice. This won't be faster that directly parsing the numbers from it (as the encoding/json package uses reflection), but it is certainly simpler: s := "[156, 100, 713]" var is []int if err :

Slice border of 2D NumPy array by integer value

你。 提交于 2020-06-02 11:06:09
问题 I would like to slice a 2D NumPy array by an integer value, but I cannot find a way to do this properly. I need to slice the "border" of the matrix by a certain number of rows/columns. Say the array is: a = np.reshape(np.arange(25),(5,5)) print a [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19] [20 21 22 23 24]] slice_val = 1 b = a[:-slice_val,:-slice_val] print "\n", b What I get is: [[ 0 1 2 3] [ 5 6 7 8] [10 11 12 13] [15 16 17 18]] , but I want something like this: [[6 7 8 ]