slice

Is there a Julia equivalent to NumPy's ellipsis slicing syntax (…)?

こ雲淡風輕ζ 提交于 2019-12-05 03:29:05
In NumPy, the ellipsis syntax is for filling in a number of : until the number of slicing specifiers matches the dimension of the array. (paraphrasing this answer ). How can I do that in Julia? Not yet, but you can help yourself if you want. import Base.getindex, Base.setindex! const .. = Val{:...} setindex!{T}(A::AbstractArray{T,1}, x, ::Type{Val{:...}}, n) = A[n] = x setindex!{T}(A::AbstractArray{T,2}, x, ::Type{Val{:...}}, n) = A[ :, n] = x setindex!{T}(A::AbstractArray{T,3}, x, ::Type{Val{:...}}, n) = A[ :, :, n] =x getindex{T}(A::AbstractArray{T,1}, ::Type{Val{:...}}, n) = A[n] getindex{T

(二十六)golang--切片

好久不见. 提交于 2019-12-05 02:41:53
基本介绍: 切片是数组的引用; 切片的使用和数组类似; 切片的长度是可以变化的; 切片的定义 var a []int,注意和数组定义的区别; 切片不仅可以使用len函数,还有cap函数来计算切片的容量; 切片在内存中的形式: 从该图我们可以得出: slice的确是一个引用类型; slice从底层来说,其实就是一个数据结构struct: type slice sruct {   ptr *[2]int   len int   cap int } 切片使用的三种方式: 即上面的对数组的操作; 通过make来创建切片;var slice []int = make([]int,4,20)三个参数分别是类型,长度,容量 定义一个切片,直接就指定数组;var slice []int = []int{1,3,5} 方式1和方式2的区别: 方式1是直接引用数组,这个数组是事先存在的,程序员是可见的;方式2通过make,make也会创建一个数组,是由切片在底层进行维护,对程序员是不可见的; 切片的遍历:与数组一样,也是有两种; 切片使用的注意事项: 切片定义后需要引用到一个数组或者make一个空间供切片使用; 切片之后可以继续切片; append内置函数,可以动态增加切片; var slice []int = []int{1,2,3} slice = append(slice,4,5,6)

Slicing a Pandas DataFrame into a new DataFrame

孤街浪徒 提交于 2019-12-05 02:41:10
I would like to slice a DataFrame with a Boolean index obtaining a copy, and then do stuff on that copy independently of the original DataFrame. Judging from this answer , selecting with .loc using a Boolean array will hand me back a copy, but then, if I try to change the copy, SettingWithCopyWarning gets in the way. Would this then be the correct way: import numpy as np import pandas as pd d1 = pd.DataFrame(np.random.randn(10, 5), columns=['a', 'b', 'c', 'd', 'e']) # create a new dataframe from the sliced copy d2 = pd.DataFrame(d1.loc[d1.a > 1, :]) # do stuff with d2, keep d1 unchanged You

Assign value to multiple slices in numpy

有些话、适合烂在心里 提交于 2019-12-05 02:21:37
In Matlab, you can assign a value to multiple slices of the same list: >> a = 1:10 a = 1 2 3 4 5 6 7 8 9 10 >> a([1:3,7:9]) = 10 a = 10 10 10 4 5 6 10 10 10 10 How can you do this in Python with a numpy array? >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> a[1:3,7:9] = 10 IndexError: too many indices a = np.arange(10) a[[range(3)+range(6,9)]] = 10 #or a[[0,1,2,6,7,8]] = 10 print a that should work I think ... I dont know that its quite what you want though You might also consider using np.r_ : http://docs.scipy.org/doc/numpy/reference/generated/numpy.r_.html ii = np.r_[0

golang 数组 slice 乱序

梦想的初衷 提交于 2019-12-05 01:48:52
数组乱序 方法1.sliceOutOfOrder 选定随机数r 将下标r和下边i交换,i– ,就是和当前最后一位交换 方法2.rand.Perm(数组长度) 将原数组下标按照随机出来的数据赋值给另一数组 package main import "fmt" import "math/rand" import "time" var ( rr = rand.New(rand.NewSource(time.Now().UnixNano())) a1 = [ 2 ] int {} a2 = [ 10 ] int {} ) func main() { a := [] int { 0 , 1 } for i := 0 ; i < 10000 ; i++ { randslice() //fmt.Println(rr.Intn(10)) sliceOutOfOrder(a) } fmt.Println(a1) fmt.Println(a2) } func sliceOutOfOrder( in [] int ) [] int { l := len( in ) for i := l - 1 ; i > 0 ; i-- { r := rr.Intn(i) in [r], in [i] = in [i], in [r] } a1[ in [ 0 ]] += 1 return in } func

Slice pandas dataframe based on datetime column

我的梦境 提交于 2019-12-05 01:48:09
问题 I have a pandas dataframe with a column as datatime that looks like: data.ts_placed Out[68]: 1 2008-02-22 15:30:40 2 2008-03-20 16:56:00 3 2008-06-14 21:26:02 4 2008-06-16 10:26:02 5 2008-06-23 20:41:03 6 2008-07-17 08:02:00 7 2008-10-13 12:47:05 8 2008-11-14 09:20:33 9 2009-02-23 11:24:18 10 2009-03-02 10:29:19 I'd like to slice the dataframe by eliminating all rows before 2009 回答1: You can use a simple string comparison to compare the values against a year string: In [63]: df.loc[df['date']

numpy : assembling multiple slices into new array

蓝咒 提交于 2019-12-05 01:06:56
问题 I have a 2-dimentional array, of which I need to extract sections (slices) into a new array: original= numpy.ndarray( shape=(4,4) ) slices= numpy.ndarray( shape=(0,2) ) for x in range(3): slice= original[x:x+2,x:x+2] slices=numpy.append(slices, slice,axis=0) Is there a more efficient way to do this (getting rid of the python for cycle)? ----EDIT---- To clarify, I'm asking how to copy arbitrarily (but similarly) shaped 2D slices from arbitrary 2D indexes of an 2D array into another, vertically

Golang之bytes.buffer

戏子无情 提交于 2019-12-05 01:06:08
bytes.buffer是一个缓冲byte类型的缓冲器存放着都是byte Buffer 是 bytes 包中的一个 type Buffer struct{…} A buffer is a variable-sized buffer of bytes with Read and Write methods. The zero value for Buffer is an empty buffer ready to use. (是一个变长的 buffer,具有 Read 和Write 方法。 Buffer 的 零值 是一个 空的 buffer,但是可以使用) Buffer 就像一个集装箱容器,可以存东西,取东西(存取数据) 创建 一个 Buffer (其实底层就是一个 []byte, 字节切片) 向其中写入数据 (Write mtheods) 从其中读取数据 (Write methods) 创建 Buffer缓冲器 var b bytes.Buffer //直接定义一个 Buffer 变量,而不用初始化 b.Writer([] byte ( "Hello " )) // 可以直接使用 b1 := new (bytes.Buffer) //直接使用 new 初始化,可以直接使用 // 其它两种定义方式 func NewBuffer(buf [] byte ) *Buffer func

Golang之bytes.buffer学习笔记

泄露秘密 提交于 2019-12-05 01:04:17
bytes.buffer是一个缓冲byte类型的缓冲器存放着都是byte Buffer 是 bytes 包中的一个 type Buffer struct{…} A buffer is a variable-sized buffer of bytes with Read and Write methods. The zero value for Buffer is an empty buffer ready to use. (是一个变长的 buffer,具有 Read 和Write 方法。 Buffer 的 零值 是一个 空的 buffer,但是可以使用) Buffer 就像一个集装箱容器,可以存东西,取东西(存取数据) 创建 一个 Buffer (其实底层就是一个 []byte, 字节切片) 向其中写入数据 (Write mtheods) 从其中读取数据 (Write methods) 创建 Buffer缓冲器 var b bytes.Buffer //直接定义一个 Buffer 变量,而不用初始化 b.Writer([]byte(“Hello “)) // 可以直接使用 b1 := new(bytes.Buffer) //直接使用 new 初始化,可以直接使用 // 其它两种定义方式 func NewBuffer(buf []byte) *Buffer func

Go reflect初探

放肆的年华 提交于 2019-12-05 00:57:53
在计算机科学中,反射是指计算机程序在运行时(Run time)可以访问、检测和修改它本 身状态或行为的一种能力。用比喻来说,那种程序能够“观察”并且修改自己的行为。要注意反射和内省(type introspection)的区别。 对应于变量,也就是围绕着它的类型(type)和值(value)进行展开。 Go的空接口概念+反射可以发挥很大的威力>_< 两个重要类型 reflect.Type(reflect.Typeof()获得) reflect.Value(reflect.Value()获得) Type的一些方法 String Name 都以字符串的形式返回结构名,只是Name包括包名 Field(index) 根据索引获得字段,返回的是StructField(字段也有类型,当然包含一个reflect.Type的字段) NumField 返回的结构的字段数 FieldByName 根据字段名获得字段 带Field的方法是获取结构体字段信息的。 type Foo struct { X string Y int } func main() { var q = Foo{} var f interface {} = q t := reflect.TypeOf(f) for i := 0 ; i < t.NumField(); i++ { field := t.Field(i) fmt