slice

Extended slice that goes to beginning of sequence with negative stride

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] ...

Ruby on Rails running on a vps slice

What are the major gotchas and tricks when running Rails on a small slice (256MB)? What is the best server setup for cramped quarters? Passenger, Nginx, Mongrel? What is the best background task processor in this kind of environment? What do you need to watch out for? I'd love to hear the stories of people who have done this and bump...

I don't understand slicing with negative bounds in Python. How is this supposed to work?

I am a newbie to Python and have come across the following example in my book that is not explained very well. Here is my print out from the interpreter: >>> s = 'spam' >>> s[:-1] 'spa' Why does slicing with no beginning bound and a '-1' return every element except the last one? Is calling s[0:-1] logically the same as calling s[:-1]?...

good primer for python slice notation

Can anyone recommend a good concise reference for the Python slice notation? I'm a seasoned programmer but new to Python and this notation needs a bit of picking up. It looks extremely powerful, but I haven't quite got my head round it and am looking for a good guide. ...

Iterating over substrings of equal size

I want to convert my String object to Enumerable of its 1-sized substrings (not chars), how can I do this efficiently in Ruby? ...

How to generate a compiler warning/error when object sliced

Hello, Gurus, I want to know if it is possible to let compiler issue a warning/error for code as following: Note: 1. Yea, it is bad programming style and we should avoid such cases - but we are dealing with legacy code and hope compiler can help identify such cases for us.) 2. I prefer a compiler option (VC++) to disable or enable ob...

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

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. ...

python create slice object from string

I'd like to create a slice object from a string; right now the only way seems through a cumbersome hacky eval statement class getslice: def __getitem__(self, idx): return idx[0] eval("getslice()[%s, 1]" %(":-1")) thanks in advance. Edit: Sorry if the original prompt was not clear, the input in this case was ":-1". The point was t...

Whats the difference between list[-1:][0] and list[len(list)-1]?

Lest say you want the last element of a python list: what is the difference between myList[-1:][0] and myList[len(myList)-1] I thought there was no difference but then I tried this >>> list = [0] >>> list[-1:][0] 0 >>> list[-1:][0] += 1 >>> list [0] >>> list[len(list)-1] += 1 >>> list [1] I was a little surprised... ...

Inverting a string in Python

I was looking for a way to print a string backwards, and after a quick search on google, I found this method: Suppose 'a' is a string variable. This will return the 'a' string backwards: a[::-1] Can anyone explain how that works? ...

What tools are there to slice a PSD?

I regularly slice web designs provided in PSD format. As a site-builder, I don't need to create graphics—I only rearrange the layers, hide ones and show others, pick colors, widths, heights, and so on. What tools are there to do that job? I know Adobe Photoshop, but it's a very expensive tool for a site-builder who doesn't want to draw ...

Javascript chop/slice/trim off last character in string

I have a string 12345.00 would like it to return 12345.0 I have looked at trim but looks only to trim whitespace and slice which I don't see how this would work. Any suggs? ...

Intercept slice operations in Python

I want to imitate a normal python list, except whenever elements are added or removed via slicing, I want to 'save' the list. Is this possible? This was my attempt but it will never print 'saving'. class InterceptedList(list): def addSave(func): def newfunc(self, *args): func(self, *args) print 'savi...

What is the equivalent of "ByteBuffer.flip" & "ByteBuffer.slice" in .NET?

Hello :) I need to port code from Java to C#. In the Java code, the methods "ByteBuffer.flip()" and "ByteBuffer.slice" is used, and I don't know how to translate this. I've read this question (http://stackoverflow.com/questions/607587/an-equivalent-of-javax-nio-buffer-flip-in-c), but although an answer is given, I cannot figure how to ...

How to slice an array in bash

(edited to fit the answer) Looking the "Array" section in the bash(1) man page, I didn't find a way to slice a bash array. So I came up with this overly complicated function: #!/bin/bash # @brief: slice a bash array # @arg1: output-name # @arg2: input-name # @args: seq args # ---------------------------------------------- function...

MATLAB excluding data outside 1 standard deviation

I'm inexperienced with MATLAB, so sorry for the newbie question: I've got a large vector (905350 elements) storing a whole bunch of data in it. I have the standard deviation and mean, and now I want to cut out all the data points that are above/below one standard deviation from the mean. I just have no clue how. From what I gather I hav...

How could I print an array slice in awk?

In a file I am reformatting, I would like to put the last column as the first and have the rest of the columns stay the same. I could do this easily in python, but thought I'd learn some awk this evening. Here is an example: (before) polk_describes:1 diatribe_that:1 #label#:negative (after) #label#:negative polk_describes:1 diatri...

right distance of background-position in CSS

Hello I worked on a CMS and I want to have diffrent Buttons for special editings. I created a small examplefile wich looks like this: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; <html xml:lang="de" lang="de" xmlns="http://www.w3.org/1999/xhtml"&gt; <head> <meta h...

Python/numpy tricky slicing problem

Hi stack overflow, I have a problem with some numpy stuff. I need a numpy array to behave in an unusual manner by returning a slice as a view of the data I have sliced, not a copy. So heres an example of what I want to do: Say we have a simple array like this: a = array([1, 0, 0, 0]) I would like to update consecutive entries in the ...

Separate multi dimensional array

Hello! I have a multi dimensional array in PHP. $f = array('one' => array(*doesntmatter*), two => array()); When I want to use it, I only want one of the arrays. (one or two or three etc) So I want to slice it into (in this case) two seperate arrays, like this: $one = array(**); $two = array(**); Can I solve this with a default fu...