slice

Numpy Dynamic Slicing Per Row

梦想的初衷 提交于 2019-12-01 10:45:34
How do I dynamically slice each row given a starting and ending index without using a for loop. I can do it with loop listed below, but it is way too slow for something where the x.shape[0] > 1 mill x= np.arange(0,100) x = x.reshape(20,5) s_idx = np.random.randint(0,3,x.shape[0]) e_idx = np.random.randint(3,6,x.shape[0]) print(s_idx) >>> array([2, 1, 2, ..., 1, 0, 2]) print(e_idx) >>> array([3, 4, 5, ..., 3, 3, 3]) print(x) >>> array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], ..., [85, 86, 87, 88, 89], [90, 91, 92, 93, 94], [95, 96, 97, 98, 99]]) x_indexed = [] for idx,value in

Why is extended slice assignment less flexible than regular slice assignment?

隐身守侯 提交于 2019-12-01 10:40:55
According to the Python documentation on extended slices : If you have a mutable sequence such as a list or an array you can assign to or delete an extended slice, but there are some differences between assignment to extended and regular slices. Assignment to a regular slice can be used to change the length of the sequence: >>> a = range(3) >>> a [0, 1, 2] >>> a[1:3] = [4, 5, 6] >>> a [0, 4, 5, 6] Extended slices aren't this flexible. When assigning to an extended slice, the list on the right hand side of the statement must contain the same number of items as the slice it is replacing: >>> a =

深入解析 Go 中 Slice 底层实现

孤人 提交于 2019-12-01 09:43:39
原文: https://halfrost.com/go_slice/ 切片是 Go 中的一种基本的数据结构,使用这种结构可以用来管理数据集合。切片的设计想法是由动态数组概念而来,为了开发者可以更加方便的使一个数据结构可以自动增加和减少。但是切片本身并不是动态数据或者数组指针。切片常见的操作有 reslice、append、copy。与此同时,切片还具有可索引,可迭代的优秀特性。 一. 切片和数组 关于切片和数组怎么选择?接下来好好讨论讨论这个问题。 在 Go 中,与 C 数组变量隐式作为指针使用不同,Go 数组是值类型,赋值和函数传参操作都会复制整个数组数据。 Go func main() { arrayA := [2]int{100, 200} var arrayB [2]int arrayB = arrayA fmt.Printf("arrayA : %p , %v\n", &arrayA, arrayA) fmt.Printf("arrayB : %p , %v\n", &arrayB, arrayB) testArray(arrayA) } func testArray(x [2]int) { fmt.Printf("func Array : %p , %v\n", &x, x) } 打印结果: Go arrayA : 0xc4200bebf0 , [100 200]

Removing a string from a slice in Go [duplicate]

核能气质少年 提交于 2019-12-01 09:40:40
问题 This question already has answers here : Delete element in a slice (6 answers) Closed 3 years ago . I have a slice of strings, and I want to remove a specific one. strings := []string strings = append(strings, "one") strings = append(strings, "two") strings = append(strings, "three") Now how can I remove the string "two" from strings ? 回答1: Find the element you want to remove and remove it like you would any element from any other slice. Finding it is a linear search. Removing is one of the

GO代码风格指南 Uber Go (转载)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 09:35:00
原文地址: https://github.com/uber-go/guide/blob/master/style.md 译文出处: https://github.com/uber-go/guide 本文永久链接: https://github.com/gocn/translator/blob/master/2019/w38_uber_go_style_guide.md 译者: 咔叽咔叽 校对者: fivezh , cvley 目录 介绍 指南 接口的指针 接收者和接口 零值 Mutexes 是有效的 复制 Slice 和 Map Defer 的使用 channel 的大小是 1 或者 None 枚举值从 1 开始 Error 类型 Error 包装 处理类型断言失败 避免 Panic 使用 go.uber.org/atomic 性能 strconv 优于 fmt 避免 string 到 byte 的转换 代码样式 聚合相似的声明 包的分组导入的顺序 包命名 函数命名 别名导入 函数分组和顺序 减少嵌套 不必要的 else 顶层变量的声明 在不可导出的全局变量前面加上 _ 结构体的嵌入 使用字段名去初始化结构体 局部变量声明 nil 是一个有效的 slice 减少变量的作用域 避免裸参数 使用原生字符串格式来避免转义 初始化结构体 在 Printf 之外格式化字符串 Printf

Removing a string from a slice in Go [duplicate]

让人想犯罪 __ 提交于 2019-12-01 09:30:44
This question already has an answer here: Delete element in a slice 6 answers I have a slice of strings, and I want to remove a specific one. strings := []string strings = append(strings, "one") strings = append(strings, "two") strings = append(strings, "three") Now how can I remove the string "two" from strings ? Find the element you want to remove and remove it like you would any element from any other slice. Finding it is a linear search. Removing is one of the following slice tricks : a = append(a[:i], a[i+1:]...) // or a = a[:i+copy(a[i:], a[i+1:])] Here is the complete solution (try it

How to efficiently index into a 1D numpy array via slice ranges

强颜欢笑 提交于 2019-12-01 09:08:36
I have a big 1D array of data. I have a starts array of indexes into that data where important things happened. I want to get an array of ranges so that I get windows of length L , one for each starting point in starts . Bogus sample data: data = np.linspace(0,10,50) starts = np.array([0,10,21]) length = 5 I want to instinctively do something like data[starts:starts+length] But really, I need to turn starts into 2D array of range "windows." Coming from functional languages, I would think of it as a map from a list to a list of lists, like: np.apply_along_axis(lambda i: np.arange(i,i+length), 0

Rust入坑指南:核心概念

纵然是瞬间 提交于 2019-12-01 09:06:00
如果说前面的坑我们一直在用小铲子挖的话,那么今天的坑就是用挖掘机挖的。 今天要介绍的是Rust的一个核心概念:Ownership。全文将分为什么是Ownership以及Ownership的传递类型两部分。 什么是Ownership 每种编程语言都有自己的一套内存管理的方法。有些需要显式的分配和回收内存(如C),有些语言则依赖于垃圾回收器来回收不使用的内存(如Java)。而Rust不属于以上任何一种,它有一套自己的内存管理规则,叫做Ownership。 在具体介绍Ownership之前,我想要先声明一点。 Rust入坑指南:常规套路 一文中介绍的数据类型,其数据都是存储在栈中。而像String或一些自定义的复杂数据结构(我们以后会对它们进行详细介绍),其数据则存储在堆内存中。明确了这一点后,我们来看下Ownership的规则有哪些。 Ownership的规则 在Rust中,每一个值都有对应的变量,这个变量称为值的owner 一个值在某一时刻只能有一个owner 当owner超出作用域后,值会被销毁 这三条规则非常重要,记住他们会帮助你更好的理解本文。 变量作用域 Ownership的规则中,有一条是owner超过范围后,值会被销毁。那么owner的范围又是如何定义的呢?在Rust中,花括号通常是变量范围作用域的标志。最常见的在一个函数中,变量s的范围从定义开始生效,直到函数结束

Unpacking slice of slices

让人想犯罪 __ 提交于 2019-12-01 08:33:19
I am curious about unpacking a slice of slices and sending them as arguments to a variadic function. Let's say we have a function with variadic parameters: func unpack(args ...interface{}) If we wan't to pass in a slice of interfaces it works, it doesn't matter if we unpack it or not: slice := []interface{}{1,2,3} unpack(slice) // works unpack(slice...) // works It gets tricky if we have a slice of slices. Here the compiler doesn't let us pass in an unpacked version: sliceOfSlices := [][]interface{}{ []interface{}{1,2}, []interface{}{101,102}, } unpack(sliceOfSlices) // works unpack

Yielding until all needed values are yielded, is there way to make slice to become lazy

假如想象 提交于 2019-12-01 08:32:06
Is there way to stop yielding when generator did not finish values and all needed results have been read? I mean that generator is giving values without ever doing StopIteration. For example, this never stops: (REVISED) from random import randint def devtrue(): while True: yield True answers=[False for _ in range(randint(100,100000))] answers[::randint(3,19)]=devtrue() print answers I found this code, but do not yet understand, how to apply it in this case: http://code.activestate.com/recipes/576585-lazy-recursive-generator-function/ You can call close() on the generator object. This way, a