slice

Why was p[:] designed to work differently in these two situations?

亡梦爱人 提交于 2019-12-03 06:13:25
问题 p = [1,2,3] print(p) # [1, 2, 3] q=p[:] # supposed to do a shallow copy q[0]=11 print(q) #[11, 2, 3] print(p) #[1, 2, 3] # above confirms that q is not p, and is a distinct copy del p[:] # why is this not creating a copy and deleting that copy ? print(p) # [] Above confirms p[:] doesnt work the same way in these 2 situations. Isn't it ? Considering that in the following code, I expect to be working directly with p and not a copy of p , p[0] = 111 p[1:3] = [222, 333] print(p) # [111, 222, 333]

Why can not convert [Size]byte to string in Go?

对着背影说爱祢 提交于 2019-12-03 06:12:12
I have a sized byte array that I got after doing md5.Sum() . data := []byte("testing") var pass string var b [16]byte b = md5.Sum(data) pass = string(b) The error: cannot convert b (type [16]byte) to type string I find the solution at this problem Change to: pass = string(b[:]) But why can not use it like this? pass = string(b) Short answer is because the Go Language Specification does not permit it. Quoting from the Go Language Specification: Conversions : A non-constant value x can be converted to type T in any of these cases: x is assignable to T . x 's type and T have identical underlying

spread syntax vs slice method

喜欢而已 提交于 2019-12-03 05:26:43
I was trying to understand what is the difference between spread syntax vs slice method in the following approach. suppose I want to make an actual copy of an array, I can probably easily do it using spread syntax var fruits = ["Banana", "Chips" , "Orange", "Lemon", "Apple", "Mango"] var newCitrus = [...fruits] If I console.log this ["Banana", "Chips", "Orange", "Lemon", "Apple", "Mango"] but I can also create a copy of an array using the slice method. Considering the same array above, if I do something like this... var citrus = fruits.slice(0); and then console log it, it will give me exactly

go-数组与切片

南楼画角 提交于 2019-12-03 05:26:32
数组与切片 定义数组 var score [5]float64 //四种初始化数组方式 var numArr01 [3]int = [3]int{1,2,3} var numArr02 = [3]int{4,5,6} var numArr03 = [...]int{8,9,10} var numArr04 = [...]int{1:800, 2:900, 3:999} //类型推导 strArr05 := [...]string{1:"tom", 0:"jack", 8:"aim"} 数组的遍历 常规遍历 略 for-range结构遍历 go语言独有的一种结构 for index, value := range arry01 {...} 第一个参数是下标,第二个是值,可用占位符_代替 都为局部变量 index和value不是固定的,可自己取名,一般这样写 数组注意事项 数组是多种数据类型的组合,一旦声明,长度固定,不能动态变化 数组中的元素可以是任意类型,包括引用类型,但是不能混用 没有赋值为默认值 数组下标必须在指定范围内使用,否则会报panic错误,数组越界 若想在其他函数中修改,可以使用引用传递(指针方式) 长度是数组的一部分,传递时需考虑长度,所以不能:arr[4]=arr[3],也不能:arr[]=arr[3] 切片介绍 引用类型,遵循引用类型传递机制 动态变化的数组

Slice of slices types

删除回忆录丶 提交于 2019-12-03 05:26:16
问题 I'm currently working my way through the excellent Tour of Go. I finished one of the exercises (#45) with the following solution: func Pic(dx, dy int) [][]uint8 { pic := make([][]uint8, dy) /* type declaration */ for i := range pic { pic[i] = make([]uint8, dx) /* again the type? */ for j := range pic[i] { pic[i][j] = uint8((i+j)/2) } } return pic } I don't understand why I have to use a make statement with the uint8 type twice (see comments in snippet). That seems redundant but I can't figure

How to get the underlying array of a slice in Go?

99封情书 提交于 2019-12-03 05:17:22
Let's say I have the following array of ints of length 3: nums := [3]int{1,2,3} Then I grab the slice of just the first two items numSlice := nums[:2] Invoking cap on numSlice and nums yields 3 in both cases, and len yields 2 and 3 respectively. If I then append to that slice ( numSlice = append(numSlice, 10) ), the underlying array ( nums ) is now [1 2 10] . cap remains at 3 for both, as the underlying array of the slice is the same, and len for the slice is now 3. However, if I append to that slice again ( numSlice = append(numSlice, 20) ), the underlying array of the slice must change - we

slicing sparse (scipy) matrix

倾然丶 夕夏残阳落幕 提交于 2019-12-03 05:12:02
问题 I would appreciate any help, to understand following behavior when slicing a lil_matrix (A) from the scipy.sparse package. Actually, I would like to extract a submatrix based on an arbitrary index list for both rows and columns. When I used this two lines of code: x1 = A[list 1,:] x2 = x1[:,list 2] Everything was fine and I could extract the right submatrix. When I tried to do this in one line, it failed (The returning matrix was empty) x=A[list 1,list 2] Why is this so? Overall, I have used

How to search for an element in a golang slice

故事扮演 提交于 2019-12-03 04:53:44
问题 I have a slice of structs. type Config struct { Key string Value string } // I form a slice of the above struct var myconfig []Config // unmarshal a response body into the above slice if err := json.Unmarshal(respbody, &myconfig); err != nil { panic(err) } fmt.Println(config) Here is the output of this: [{key1 test} {web/key1 test2}] How can I search this array to get the element where key="key1" ? 回答1: With a simple for loop: for _, v := range myconfig { if v.Key == "key1" { // Found! } }

Create a slice using a tuple

孤街浪徒 提交于 2019-12-03 04:47:30
Is there any way in python to use a tuple as the indices for a slice? The following is not valid: >>> a = range(20) >>> b = (5, 12) # my slice indices >>> a[b] # not valid >>> a[slice(b)] # not valid >>> a[b[0]:b[1]] # is an awkward syntax [5, 6, 7, 8, 9, 10, 11] >>> b1, b2 = b >>> a[b1:b2] # looks a bit cleaner [5, 6, 7, 8, 9, 10, 11] It seems like a reasonably pythonic syntax so I am surprised that I can't do it. (update) And the solution turns out to be: >>> a[slice(*b)] [5, 6, 7, 8, 9, 10, 11] You can use Python's *args syntax for this: >>> a = range(20) >>> b = (5, 12) >>> a[slice(*b)] [5

Idiomatic sequence slice in Clojure

做~自己de王妃 提交于 2019-12-03 04:16:18
问题 In Python, there is a convenient way of taking parts of a list called "slicing": a = [1,2,3,4,5,6,7,8,9,10] # ≡ a = range(1,10) a[:3] # get first 3 elements a[3:] # get all elements except the first 3 a[:-3] # get all elements except the last 3 a[-3:] # get last 3 elements a[3:7] # get 4 elements starting from 3rd (≡ from 3rd to 7th exclusive) a[3:-3] # get all elements except the first 3 and the last 3 Playing with clojure.repl/doc in Clojure, I found equivalents for all of them but I'm not