slice

Preserving the dimensions of a slice from a Numpy 3d array

倖福魔咒の 提交于 2019-11-30 08:30:00
问题 I have a 3d array, a , of shape say a.shape = (10, 10, 10) When slicing, the dimensions are squeezed automatically i.e. a[:,:,5].shape = (10, 10) I'd like to preserve the number of dimensions but also ensure that the dimension that was squeezed is the one that shows 1 i.e. a[:,:,5].shape = (10, 10, 1) I have thought of re-casting the array and passing ndmin but that just adds the extra dimensions to the start of the shape tuple regardless of where the slice came from in the array a . 回答1: a[:

Discontinuous slice in python list

梦想与她 提交于 2019-11-30 08:22:25
I'm looking for an efficient way of achieving this, which I think is a slicing-like operation: >>> mylist = range(100) >>>magicslicer(mylist, 10, 20) [0,1,2,3,4,5,6,7,8,9,30,31,32,33,34,35,36,37,38,39,60,61,62,63......,97,98,99] the idea is: the slicing gets 10 elements, then skips 20 elements, then gets next 10, then skips next 20, and so on. I think I should not use loops if possible, for the very reason to use slice is (I guess) to do the "extraction" efficiently in a single operation. Thanks for reading. itertools.compress (new in 2.7/3.1) nicely supports use cases like this one,

What is the difference between slice() and substr() in JavaScript?

一笑奈何 提交于 2019-11-30 08:18:34
Can I ask what the difference is between string object slice() and substr() in JavaScript? They have different signatures, .slice() is: string.slice(beginIndex, endIndex) Whereas .substr() is: string.substr(beginIndex, length); So for example, if we have "1234" and wanted "23" , it would be: "1234".slice(1,3) //or... "1234".substr(1,2) They also have different behavior for the more-rarely used negative indexes, look at the MDC documentation for .slice() and .substr() for full descriptions. String.slice(begin, end) This method will cut text from begin to end char, eg.: alert("Hello World!"

What's the best way to get the last N elements of a Perl array?

旧街凉风 提交于 2019-11-30 08:17:47
What's the best way to get the last N elements of a Perl array? If the array has less than N, I don't want a bunch of undefs in the return value. @last_n = @source[-$n..-1]; If you require no undef s, then: @last_n = ($n >= @source) ? @source : @source[-$n..-1]; siukurnin I think what you want is called a slice . @a = (a .. z); @last_five = @a[ $#a - 4 .. $#a ]; say join " ", @last_five; outputs: v w x y z simple, no math: @a = reverse @a; @a = splice(@a, 0, $elements_to_keep); @a = reverse @a; As @a in scalar context gives the length on an array a and because @a == $#a + 1 (unless $[ is set

Slice like functionality from a List in F#

匆匆过客 提交于 2019-11-30 07:55:44
问题 With an array let foo = [|1;2;3;4|] I can use any of the following to return a slice from an array. foo.[..2] foo.[1..2] foo.[2..] How can I do the same thing for List let foo2 = [1;2;3;4] ? When I try the same syntax as the array I get error FS00039: The field, constructor or member 'GetSlice' is not defined. What's the preferred method of getting a subsection of a List and why aren't they built to support GetSlice? 回答1: What's the preferred method of getting a subsection of a List and why

PHP进程间通信

爷,独闯天下 提交于 2019-11-30 07:41:12
PHP作为解释器运行通过线程或者进程都能实现(如果使用Apache,那么就可能使用多线程模型。使用php-fpm,就是使用多进程模型,这里以多进程模型解释)。服务器每接收到一个请求就要起一个PHP进程,平均一个PHP进程消耗内存2M左右(默认最大为8M,参数可以设置)。独立的进程让PHP能专一的做自己的解释工作,程序员也从复杂的代码逻辑中走出来,不用担心资源的竞争和各种锁问题。独立进程虽好但这也导致想通过多进程或者异步来提速成本非常的高(主要是开发难度)。如果一定要通过PHP实现多进程和异步其实是很容易做到的。 PHP有很多第三方扩展,比如Swoole能让PHP像Node一样实现异步。PHP官方扩展库 pcntl_* 能很简单的实现多进程。扩展虽好,但实际应用时切忌要慎重,便利的同时风险也来了。比如对多进程的控制,处理不好很容易导致程序死锁,CPU内存爆表、服务器宕机。异步回调的Coding方式与PHP本身的编程思想有一定出入,驾驭不好也是灾难。 当然也不能说的太吓人,在实际的项目中我们有很多场景不得不考虑通过多进程或者异步来优化程序。这里举一个很常见的例子 『发送消息通知』 ,比如短信和邮件。这里说一个实际的场景:企业需要给200W用户发短信通知 ,短信接口支持最大100次/秒的调用频率 ,短信接口每次调用耗时300毫秒。如果单进程跑脚本的话,需要7天才能把短信发完。

Slicing: Out of bounds error in Go

☆樱花仙子☆ 提交于 2019-11-30 07:28:36
问题 package main import "fmt" func main() { a := make([]int, 5) printSlice("a", a) b := make([]int, 0, 5) printSlice("b", b) c := b[1:] printSlice("c", c) } func printSlice(s string, x []int) { fmt.Printf("%s len=%d cap=%d %v\n", s, len(x), cap(x), x) } The above gives me an out of bounds error: a len=5 cap=5 [0 0 0 0 0] b len=0 cap=5 [] panic: runtime error: slice bounds out of range goroutine 1 [running]: main.main() /private/var/folders/q_/53gv6r4s0y5f50v9p26qhs3h00911v/T/compile117.go:10

Passing Python slice syntax around to functions

倖福魔咒の 提交于 2019-11-30 07:25:47
问题 In Python, is it possible to encapsulate exactly the common slice syntax and pass it around? I know that I can use slice or __slice__ to emulate slicing. But I want to pass the exact same syntax that I would put in the square brackets that would get used with __getitem__ . For example, suppose I wrote a function to return some slice of a list. def get_important_values(some_list, some_condition, slice): elems = filter(some_condition, some_list) return elems[slice] This works fine if I manually

Python Pandas slice multiindex by second level index (or any other level)

放肆的年华 提交于 2019-11-30 07:16:55
There are many postings on slicing the level[0] of a multiindex by a range of level 1 . However, I cannot find a solution for my problem; that is, I need a range of the level 1 index for level[0] index values dataframe: First is A to Z, Rank is 1 to 400; I need the first 2 and last 2 for each level[0] (First), but not in the same step. Title Score First Rank A 1 foo 100 2 bar 90 3 lime 80 4 lame 70 B 1 foo 400 2 lime 300 3 lame 200 4 dime 100 I am trying to get the last 2 rows for each level 1 index with the below code, but it slices properly only for the first level[0] value. [IN] df.ix[x

Extended slice that goes to beginning of sequence with negative stride

我的未来我决定 提交于 2019-11-30 06:50:20
Bear with me while I explain my question. Skip down to the bold heading if you already understand extended slice list indexing. In python, you can index lists using slice notation. Here's an example: >>> A = list(range(10)) >>> A[0:5] [0, 1, 2, 3, 4] You can also include a stride, which acts like a "step": >>> A[0:5:2] [0, 2, 4] The stride is also allowed to be negative, meaning the elements are retrieved in reverse order: >>> A[5:0:-1] [5, 4, 3, 2, 1] But wait! I wanted to see [4, 3, 2, 1, 0] . Oh, I see, I need to decrement the start and end indices: >>> A[4:-1:-1] [] What happened? It's