slice

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

岁酱吖の 提交于 2019-12-10 03:54:22
问题 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? 回答1: 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

Assign value to multiple slices in numpy

依然范特西╮ 提交于 2019-12-10 02:28:31
问题 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 回答1: 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 回答2: You

why slice values can sometimes go stale but never map values?

主宰稳场 提交于 2019-12-09 23:52:23
问题 I find that slice map function and channel are frequently mentioned together as reference types . However I notice that slices somethings exhibit none reference behavior like they can go stale: var s []int //must update slice value s = append(s, ...) or //must use pointer if we want to expose the change func foo(s *[]int) error //or change the function signature to return it like _append_ func foo(s []int) (r slice, err error) Usually I understand this by keeping the inner components of the

Reverse string Python slice notation

半世苍凉 提交于 2019-12-09 19:00:11
问题 string = "HELLO" print string[::-1] #as expected print string[0:6:-1] #empty string why ? I was amazed to see how easy it is to reverse a string in python but then I struck upon this and got lost. Can someone please explain why the second reverse does not works ? 回答1: The reason the second string is empty is because you are telling the compiler to begin at 0, end at 6 and step -1 characters each time. Since the compiler will never get to a number bigger than six by repeatedly adding -1 to 0

slice(start, [end]) 选取一个匹配的子集 与原来的slice方法类似

一个人想着一个人 提交于 2019-12-09 18:05:53
slice(start, [end] ) 概述 选取一个匹配的子集 与原来的slice方法类似 参数 start Integer V1.1.4 开始选取子集的位置。第一个元素是0.如果是负数,则可以从集合的尾部开始选起。 大理石平台检定规程 end Integer V1.1.4 结束选取自己的位置,如果不指定,则就是本身的结尾。 示例 描述: 选择第一个p元素 HTML 代码: <p>Hello</p><p>cruel</p><p>World</p> jQuery 代码: $("p").slice(0, 1).wrapInner("<b></b>"); 结果: [ <p><b>Hello</b></p> ] 描述: 选择前两个p元素 HTML 代码: <p>Hello</p><p>cruel</p><p>World</p> jQuery 代码: $("p").slice(0, 2).wrapInner("<b></b>"); 结果: [ <p><b>Hello</b></p>,<p><b>cruel</b></p> ] 描述: 只选取第二个p元素 HTML 代码: <p>Hello</p><p>cruel</p><p>World</p> jQuery 代码: $("p").slice(1, 2).wrapInner("<b></b>"); 结果: [ <p><b>cruel</b>

How to avoid inconsistent s[i:-j] slicing behaviour when j is sometimes 0?

只愿长相守 提交于 2019-12-09 16:45:41
问题 I am creating a number of slices [-WINDOW-i:-i] of a list, where i ranges between 32 and 0 : vals = [] for i in range(32, -1, -1): vals.append(other_list[-WINDOW-i:-i]) When i == 0 , this returns a slice of length 0: other_list[-WINDOW-0:0] I don't want to have to do this to solve it: vals = [] for i in range(32, -1, -1): if i == 0: vals.append(other_list[-WINDOW:]) else: vals.append(other_list[-WINDOW-i:-i]) … because if I have many lists to append to vals , it gets messy. Is there a clean

What is the Slice compare logic in Swift

狂风中的少年 提交于 2019-12-09 15:34:32
问题 Here's some code: var arr1 = [1, 2, 3, 4] var arr2 = [1, 2, 3, 4] if arr1 == arr2 { println("Equal") } else { println("Not Equal") } // console output: Equal let slice1 = arr1[0..4] let slice2 = arr2[0..4] if slice1 == slice2 { println("Equal") } else { println("Not Equal") } // console output: Equal It's straightforward, but code following: if arr1[0..4] == arr2[0..4] { println("Equal") } else { println("Not Equal") } // console output: Not Equal Why the result is "Not Equal" rather than

Function apply with arguments using slice

倾然丶 夕夏残阳落幕 提交于 2019-12-09 14:52:29
问题 Looking at this tutorial I saw the following code suggestion in one comment: init:function(callback){ var that =this ; return $http.jsonp(this.url).success( function(data){ that.availableGenres = that.getGenres(data); that.results = that.getResults(data); if(callback)callback.apply(null,[].slice.call(arguments)) } ) } But this line callback.apply(null,[].slice.call(arguments)) looks weird to me. Why not just: callback.apply(null, arguments) ? Because I don't like when I don't understand the

Pandas Dataframe datetime slicing with Index vs MultiIndex

拥有回忆 提交于 2019-12-09 13:37:03
问题 With single indexed dataframe I can do the following: df2 = DataFrame(data={'data': [1,2,3]}, index=Index([dt(2016,1,1), dt(2016,1,2), dt(2016,2,1)])) >>> df2['2016-01 : '2016-01'] data 2016-01-01 1 2016-01-02 2 >>> df2['2016-01-01' : '2016-01-01'] data 2016-01-01 1 Date time slicing works when you give it a complete day (i.e. 2016-01-01), and it also works when you give it a partial date, like just the year and month (2016-01). All this works great, but when you introduce a multiindex, it

Golang 中的指针 - Pointer

不想你离开。 提交于 2019-12-09 11:22:58
Golang 中的指针 - Pointer Go 的原生数据类型可以分为基本类型和高级类型,基本类型主要包含 string, bool, int 及 float 系列,高级类型包含 struct,array/slice,map,chan, func 。 相比 Java,Python,Javascript 等引用类型的语言,Golang 拥有类似C语言的指针这个相对古老的特性。但不同于 C 语言,Golang 的指针是单独的类型,而不是 C 语言中的 int 类型,而且也不能对指针做整数运算。从这一点看,Golang 的指针基本就是一种引用。 那么 Golang 为什么需要指针?这种指针又能有什么独特的用途呢? 在学习引用类型语言的时候,总是要先搞清楚,当给一个函数/方法传参的时候,传进去的是值还是引用。实际上,在大部分引用型语言里,参数为基本类型时,传进去的大都是值,也就是另外复制了一份参数到当前的函数调用栈。参数为高级类型时,传进去的基本都是引用。这个主要是因为虚拟机的内存管理导致的。 内存管理中的内存区域一般包括 heap 和 stack, stack 主要用来存储当前调用栈用到的简单类型数据:string,boolean,int,float 等。这些类型的内存占用小,容易回收,基本上它们的值和指针占用的空间差不多,因此可以直接复制,GC也比较容易做针对性的优化。