slice

Make an object that behaves like a slice

匆匆过客 提交于 2019-12-04 05:29:46
How can we make a class represent itself as a slice when appropriate? This didn't work: class MyThing(object): def __init__(self, start, stop, otherstuff): self.start = start self.stop = stop self.otherstuff = otherstuff def __index__(self): return slice(self.start, self.stop) Expected output: >>> thing = MyThing(1, 3, 'potato') >>> 'hello world'[thing] 'el' Actual output: TypeError: __index__ returned non-(int,long) (type slice) Inheriting from slice doesn't work either. MisterMiyagi TLDR: It's impossible to make custom classes replace slice for builtins types such as list and tuple . The _

How to enumerate a slice using the original indices?

我们两清 提交于 2019-12-04 04:52:13
If I want to enumerate an array (say for a map() function where I would need to use the index of an element together with its value), I could use enumerate() function. E.g.: import Foundation let array: [Double] = [1, 2, 3, 4] let powersArray = array.enumerate().map() { pow($0.element, Double($0.index)) } print("array == \(array)") print("powersArray == \(powersArray)") // array == [1.0, 2.0, 3.0, 4.0] // powersArray == [1.0, 2.0, 9.0, 64.0] <- As expected Now, if I want to use some sub-sequence from the array, I could use a slice , and it would allow me to use just the same indices as I would

Slice NSArray from end of array

点点圈 提交于 2019-12-04 04:26:43
What is the best way to "slice" an NSArray from the end, rather than the beginning, of the array (for example, finding the subarray containing the last few elements of a NSArray of unknown length)? In Python, you can use negative indices to accomplish this, e.g.: new_list = old_list[-5:-3] What's the most natural way to do this in Objective-C? There's nothing to match Python's nice syntax for this, but you could do: NSUInteger count = [myArray count]; NSArray * slice = [myArray subarrayWithRange:(NSRange){count-n, n}]; You could also write up a category for NSArray , something like: @interface

Slicing with a logical (boolean) expression a Pandas Dataframe

一笑奈何 提交于 2019-12-04 04:26:26
问题 I am getting an exception as I try to slice with a logical expression my Pandas dataframe. My data have the following form: df GDP_norm SP500_Index_deflated_norm Year 1980 2.121190 0.769400 1981 2.176224 0.843933 1982 2.134638 0.700833 1983 2.233525 0.829402 1984 2.395658 0.923654 1985 2.497204 0.922986 1986 2.584896 1.09770 df.info() <class 'pandas.core.frame.DataFrame'> Int64Index: 38 entries, 1980 to 2017 Data columns (total 2 columns): GDP_norm 38 non-null float64 SP500_Index_deflated

python Modifying slice of list in function

久未见 提交于 2019-12-04 04:26:20
问题 Consider the following piece of code: def func1(a): a[:] = [x**2 for x in a] a = range(10) print a #prints [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] func1(a[:5]) print a #also prints [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] I wish to send a slice of the list a and change it inside the function. My expected output is [0, 1, 4, 9, 16, 5, 6, 7, 8, 9] Which way is the idiomatic way to do so? Thanks! 回答1: If you slice the list, you modify only a copy, so what you want to do doesn't work in the form you want. But you

Complex list slice/index in python

馋奶兔 提交于 2019-12-04 03:43:19
I have a list that looks like this: lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] I'd like to generate a filtered list that looks like this: filtered_lst = [2, 6, 7, 9, 10, 13] Does Python provide a convention for custom slicing. Something such as: lst[1, 5, 6, 8, 9, 12] # slice a list by index Use operator.itemgetter() : from operator import itemgetter itemgetter(1, 5, 6, 8, 9, 12)(lst) Demo: >>> from operator import itemgetter >>> lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] >>> itemgetter(1, 5, 6, 8, 9, 12)(lst) (2, 6, 7, 9, 10, 13) This returns a tuple; cast to a list with list

Efficient appending to a variable-length container of strings (Golang)

江枫思渺然 提交于 2019-12-04 03:31:53
问题 The problem: I need to apply multiple regexes to each line of a big log file (like several GB long) , gather non-empty matches and put them all in an array (for serialization and sending it over the network). Slices are not much help if answer to this question holds: If the slice does not have sufficient capacity, append will need to allocate new memory and copy the old one over. For slices with <1024 elements, it will double the capacity, for slices with >1024 elements it will increase it by

Python: shorter syntax for slices with gaps?

我与影子孤独终老i 提交于 2019-12-04 03:08:48
Suppose I want the first element, the 3rd through 200th elements, and the 201st element through the last element by step-size 3, from a list in Python. One way to do it is with distinct indexing and concatenation: new_list = old_list[0:1] + old_list[3:201] + old_list[201::3] Is there a way to do this with just one index on old_list ? I would like something like the following (I know this doesn't syntactically work since list indices cannot be lists and since Python unfortunately doesn't have slice literals ; I'm just looking for something close): new_list = old_list[[0, 3:201, 201::3]] I can

numpy 3D-image array to 2D

孤者浪人 提交于 2019-12-04 01:58:10
I have a 3D-numpy array of a gray image, which looks something like this: [[[120,120,120],[67,67,67]]...] Obviously I have every R G and B the same because it is a gray image - this is redundent. I want to get a new 2D array which looks like: [[120,67]...] Which means to take every pixel's array [x,x,x] to just the value x How can I do that? If the shape of your ndarray is (M, N, 3), then you can get an (M, N) gray-scale image like this: >>> gray = img[:,:,0] 来源: https://stackoverflow.com/questions/14365029/numpy-3d-image-array-to-2d

Numpy Dynamic Slicing Per Row

安稳与你 提交于 2019-12-04 01:46:32
问题 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],