slice

My object is not updated even if I use the pointer to a type to update it

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 09:55:28
问题 I store some Individual objects in a slice. Before appending it to the slice I print the name of the Individual object. After I have stored it in the slice, I then retrieve it as a pointer and want to change the name to "Peter" , but the change does not work since it still prints "Steve" . Why? type Individual interface { GetName() *string SetName(name string) } type Person struct { name string } // Implement functions of the Individual interface func (p Person) GetName() *string { return &p

Can I concurrently write different slice elements

喜欢而已 提交于 2019-12-17 09:47:48
问题 I have a slice that contains work to be done, and a slice that will contain the results when everything is done. The following is a sketch of my general process: var results = make([]Result, len(jobs)) wg := sync.WaitGroup{} for i, job := range jobs { wg.Add(1) go func(i int, j job) { defer wg.Done() var r Result = doWork(j) results[i] = r }(i, job) } wg.Wait() // Use results It seems to work, but I have not tested it thoroughly and am not sure if it is safe to do. Generally I would not feel

What are the default slice indices in Python *really*?

孤者浪人 提交于 2019-12-17 07:38:00
问题 From the python documentation docs.python.org/tutorial/introduction.html#strings: Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced. For the standard case, this makes a lot of sense: >>> s = 'mystring' >>> s[1:] 'ystring' >>> s[:3] 'mys' >>> s[:-2] 'mystri' >>> s[-1:] 'g' >>> So far, so good. However, using a negative step value seems to suggest slightly different defaults: >>> s[:3:-1] 'gnir' >

Linq to Objects - return pairs of numbers from list of numbers

爷,独闯天下 提交于 2019-12-17 06:54:50
问题 var nums = new[]{ 1, 2, 3, 4, 5, 6, 7}; var pairs = /* some linq magic here*/ ; => pairs = { {1, 2}, {3, 4}, {5, 6}, {7, 0} } The elements of pairs should be either two-element lists, or instances of some anonymous class with two fields, something like new {First = 1, Second = 2} . 回答1: None of the default linq methods can do this lazily and with a single scan. Zipping the sequence with itself does 2 scans and grouping is not entirely lazy. Your best bet is to implement it directly: public

How to get memory size of variable in Go?

こ雲淡風輕ζ 提交于 2019-12-17 05:12:34
问题 I am curious about the memory cost of map and slice , so I wrote a program to compare the sizes. I get the memory size by unsafe.Sizeof(s) , but obviously it is wrong, because when I change the size, the output is the same. func getSlice(size int) []int { t := time.Now() s := make([]int, size*2) for i := 0; i < size; i++ { index := i << 1 s[index] = i s[index+1] = i } fmt.Println("slice time cost: ", time.Since(t)) return s } func getMap(size int) map[int]int { t := time.Now() m := make(map

Why are slice and range upper-bound exclusive?

懵懂的女人 提交于 2019-12-17 04:30:16
问题 Disclaimer: I am not asking if the upper-bound stop argument of slice() and range() is exclusive or how to use these functions. Calls to the range and slice functions, as well as the slice notation [start:stop] all refer to sets of integers. range([start], stop[, step]) slice([start], stop[, step]) In all these, the stop integer is excluded. I am wondering why the language is designed this way. Is it to make stop equal to the number of elements in the represented integer set when start equals

How to extract an arbitrary line of values from a numpy array?

女生的网名这么多〃 提交于 2019-12-17 03:22:46
问题 I have a numpy array that contains some image data. I would like to plot the 'profile' of a transect drawn across the image. The simplest case is a profile running parallel to the edge of the image, so if the image array is imdat , then the profile at a selected point (r,c) is simply imdat[r] (horizontal) or imdat[:,c] (vertical). Now, I want to take as input two points (r1,c1) and (r2,c2) , both lying inside imdat . I would like to plot the profile of the values along the line connecting

浅谈布隆过滤器Bloom Filter

馋奶兔 提交于 2019-12-16 17:15:28
先从一道面试题开始: 给A,B两个文件,各存放50亿条URL,每条URL占用64字节,内存限制是4G,让你找出A,B文件共同的URL。 这个问题的本质在于判断一个元素是否在一个集合中。哈希表以O(1)的时间复杂度来查询元素,但付出了空间的代价。在这个大数据问题中,就算哈希表有100%的空间利用率,也至少需要50亿*64Byte的空间,4G肯定是远远不够的。 当然我们可能想到使用位图,每个URL取整数哈希值,置于位图相应的位置上。4G大概有320亿个bit,看上去是可行的。但位图适合对海量的、 取值分布很均匀 的集合去重。位图的空间复杂度是随集合内最大元素增大而线性增大的。要设计冲突率很低的哈希函数,势必要增加哈希值的取值范围,假如哈希值最大取到了2 64 ,位图大概需要23亿G的空间。4G的位图最大值是320亿左右,为50亿条URL设计冲突率很低、最大值为320亿的哈希函数比较困难。 题目的一个解决思路是将文件切割成可以放入4G空间的小文件,重点在于A与B两个文件切割后的小文件要一一对应。 分别切割A与B文件,根据 hash(URL) % k 值将URL划分到k个不同的文件中,如A1,A2,...,Ak和B1,B2,...,Bk,同时可以保存hash值避免重复运算。这样Bn小文件与A文件共同的URL肯定会分到对应的An小文件中。读取An到一个哈希表中,再遍历Bn

Go语言 4 数组、切片和映射

核能气质少年 提交于 2019-12-16 15:06:05
文章由作者马志国在博客园的原创,若转载请于明显处标记出处:http://www.cnblogs.com/mazg/ 数组是由同构的元素组成。结构体是由异构的元素组成。数据和结构体都是有固定内存大小的数据结构。相比之下,切片和映射则是动态的数据结构,它们根据需要动态增长。 4.1 数组 数组是一系列同一类型数据的集合,数组中包含的每个数据被称为数组元素。一个数组包含的元素个数称为数组的长度,数组长度是固定的。一个数组可以由零个或多个元素组成。 1 数组声明 数组声明的一般形式: var 数组名 [ 长度 ] 类型 示例如下: var arr [10]int //10个元素的整型数组 var ptrs [5]*float64 //5个元素的指针数组,每个指针都指向float64类型 var points [8] struct { x, y int } //8个元素的结构体类型 var arry [2][3]int //2*3的二维整型数组 2 简短声明 与变量的简短声明一样,数组也可以简短声明。如果在数组的长度位置出现的是 “ ... ”省略号,则表示数据的长度是根据初始化值得个数来计算。 a := [3]int{1, 2, 3} // 长度为3的数组 b := [5]int{1, 2, 3} //长度为10,前三个元素为1、2、3,其它默认为0 c := [...]int{4, 5,

go 中的slice与数组,slice的append与扩容

℡╲_俬逩灬. 提交于 2019-12-16 00:53:16
转载: https://www.cnblogs.com/JenningsMao/p/9327758.html 一直没发现数组与slice的区别,发现数组的命名是[...] int或[3]int,即前面的[]要有东西,而slice直接是[]int,或者make([]int) append一定要有返回值, 当len = = cap时 append会导致扩容,底层数组不再共享, 在1024字节以内,扩容一倍,大于2014时,增加cap的1/4 go中的数组与C语言中的数组类似,但是不同的是C中的数组名其实还是指针,在作为参数传递的过程中会退化为指针,而go语言则是在编译期间就确定其大小,然后始终是作为值传递的。 初始化 [5] int {1,2,3,4,5} 长度为5的数组,其元素值依次为:1,2,3,4,5 [5] int {1,2} 长度为5的数组,其元素值依次为:1,2,0,0,0 。在初始化时没有指定初值的元素将会赋值为其元素类型int的默认值0,string的默认值是 "" [...] int {1,2,3,4,5} 长度为5的数组,其长度是根据初始化时指定的元素个数决定的 [5] int { 2:1,3:2,4:3} 长度为5的数组,key:value,其元素值依次为:0,0,1,2,3。在初始化时指定了2,3,4索引中对应的值:1,2,3 [...] int {2:1,4