slice

Declare slice or make slice?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 19:30:13
In Golang, what is the difference between var s []int and s := make([]int, 0) ? I find that both works, but which one is better? VonC In addition to fabriziom 's answer , you can see more examples at " Go Slices: usage and internals ", where a use for []int is mentioned: Since the zero value of a slice ( nil ) acts like a zero-length slice , you can declare a slice variable and then append to it in a loop: // Filter returns a new slice holding only // the elements of s that satisfy f() func Filter(s []int, fn func(int) bool) []int { var p []int // == nil for _, v := range s { if fn(v) { p =

How to find out element position in slice?

可紊 提交于 2019-11-29 19:13:36
How does one determine the position of an element present in slice? I need something like the following: type intSlice []int func (slice intSlice) pos(value int) int { for p, v := range slice { if (v == value) { return p } } return -1 } Sorry, there's no generic library function to do this. Go doesn't have a straight forward way of writing a function that can operate on any slice. Your function works, although it would be a little better if you wrote it using range . If you happen to have a byte slice, there is bytes.IndexByte . You can create generic function in idiomatic go way: func

How do you clear a slice in Go?

余生颓废 提交于 2019-11-29 18:54:17
What is the appropriate way to clear a slice in Go? Here's what I've found in the go forums : // test.go package main import ( "fmt" ) func main() { letters := []string{"a", "b", "c", "d"} fmt.Println(cap(letters)) fmt.Println(len(letters)) // clear the slice letters = letters[:0] fmt.Println(cap(letters)) fmt.Println(len(letters)) } Is this correct? To clarify, the buffer is cleared so it can be reused. An example is Buffer.Truncate function in the bytes package. Notice that Reset just calls Truncate(0). So it appears that in this case line 70 would evaluate: b.buf = b.buf[0 : 0] http:/

Python: Numpy slicing indepth explnation [closed]

谁说胖子不能爱 提交于 2019-11-29 18:30:01
I found the following code in a conway's game of life clone. I don't understand how exactly the following code is run. Can someone give an in depth explanation in how the Code is executed? def iterate(Z): # find number of neighbors that each square has N = np.zeros(Z.shape) N[1:, 1:] += Z[:-1, :-1] N[1:, :-1] += Z[:-1, 1:] N[:-1, 1:] += Z[1:, :-1] N[:-1, :-1] += Z[1:, 1:] N[:-1, :] += Z[1:, :] N[1:, :] += Z[:-1, :] N[:, :-1] += Z[:, 1:] N[:, 1:] += Z[:, :-1] # a live cell is killed if it has fewer than 2 or more than 3 neighbours. part1 = ((Z == 1) & (N < 4) & (N > 1)) # a new cell forms if a

get the count of elements of tuples of your own…not just the range or sequence

微笑、不失礼 提交于 2019-11-29 18:21:39
The below code is running for first three elements of the tuple of this list SS1=[(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)] from collections import Counter c = Counter(elem[0:3] for elem in SS1) for k, v in c.items(): if (v > 0): print(k,v) and the output is: (1, 2, 3) 3 (1, 2, 4) 1 (1, 3, 4) 1 (2, 3, 4) 1 But my expectation is not just for first three tuple...i want the counter for tuple (0,2,3) or tuple (1,2,4) likewise i can pass any three position of the tuple and get the count of it... How can I do this? If what i understood from

Copying a subset of an array into another array / array slicing in C

▼魔方 西西 提交于 2019-11-29 18:13:59
问题 In C, is there any built-in array slicing mechanism? Like in Matlab for example, A(1:4) would produce = 1 1 1 1 How can I achieve this in C? I tried looking, but the closest I could find is this: http://cboard.cprogramming.com/c-programming/95772-how-do-array-subsets.html subsetArray = &bigArray[someIndex] But this does not exactly return the sliced array, instead pointer to the first element of the sliced array... Many thanks 回答1: Doing that in std C is not possible. You have to do it

How can I create an array that contains unique strings?

懵懂的女人 提交于 2019-11-29 18:11:53
I want to create an array that contains unique strings. How can I do that? var paths = make([]string, 0) func main() { // Members are added dynamically paths = append(paths, "aaa") paths = append(paths, "bbb") paths = append(paths, "bbb") paths = append(paths, "ccc") // convert ["aaa", "bbb", "bbb", "ccc"] -> ["aaa", "bbb", "ccc"] // or can I use some class that disallow the same string automaticaly? } If you want a collection of unique elements, that is the Set data type. Go does not have a set data type, but you can use a map[string]bool to act as a set. For a "nice" set, use a map with bool

specific kind of slicing over tensor object in tensorflow

这一生的挚爱 提交于 2019-11-29 17:46:42
Summary of the question, Is this kind of slicing and then assignment supported in tensorflow? out[tf_a2[y],x[:,None]] = tf_a1[tf_a2[y],x[:,None]] final = out[:-1] Lets give the example, I have a tensor like this: tf_a1 = tf.Variable([ [9.968594, 8.655439, 0., 0. ], [0., 8.3356, 0., 8.8974 ], [0., 0., 6.103182, 7.330564 ], [6.609862, 0., 3.0614321, 0. ], [9.497023, 0., 3.8914037, 0. ], [0., 8.457685, 8.602337, 0. ], [0., 0., 5.826657, 8.283971 ], [0., 0., 0., 0. ]]) and I have this one: tf_a2 = tf.constant([[1, 2, 5], [1, 4, 6], [0, 7, 7], [2, 3, 6], [2, 4, 7]]) Now I want to keep the elements

JavaScript基础之数组常用方法

本小妞迷上赌 提交于 2019-11-29 17:14:43
目录 JS 数组常用API 常用属性 常用方法 常见方法语法解释 from方法 isArray concat every fill filter find forEach indexOf join keys map pop reduce reverse slice some sort splice JS 数组常用API 常用属性 length 属性 prototype 常用方法 from 从一个类似数组或可迭代对象中创建一个新的数组实例。 isArray 用于确定传递的值是否是一个 Array。 of 创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型。 concat 用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。 copyWithin 浅复制数组的一部分到同一数组中的另一个位置,并返回它,不会改变原数组的长度。 entries 返回一个新的Array Iterator对象,该对象包含数组中每个索引的键/值对。 every 测试数组的所有元素是否都通过了指定函数的测试。 fill 用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引。 filter 创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。 find 返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined。 findIndex

Why can't Go slice be used as keys in Go maps pretty much the same way arrays can be used as keys?

被刻印的时光 ゝ 提交于 2019-11-29 16:56:51
Why can't Go slice (which is an implementation of Go arrays) be used as keys in Go maps pretty much the same way arrays can be used as keys? Here's Nigel Tao's answer from https://groups.google.com/forum/#!topic/golang-nuts/zYlx6sR4F8Y : One reason is that arrays are value types. If a0 is an [N]int (an array) then doing a1 := a0 a1[0] = 0 will not affect a0[0] at all. In comparison, slices refer to an underlying array. Copying a slice value is O(1) instead of O(length). If s0 is an []int (a slice) then doing s1 := s0 s1[0] = 0 will affect what s0[0] is. http://play.golang.org/p/TVkntIsLo8 Map