slice

Can python's slice notation be used outside of brackets?

断了今生、忘了曾经 提交于 2019-12-07 18:35:18
问题 Within brackets, python's slice shorthand auto-generates tuples of slice objects: class Foo(object): def __getitem__(self, key): print key Foo()[1::, 2:20:5] This prints (slice(1, None, None), slice(2, 20, 5)) . As far as I can tell, however, this shorthand doesn't work outside brackets. Is there any way to use slice shorthand in other contexts? I could define a dummy object that simply returns whatever it is passed to __getitem__ -- that would at least give me a way to generate slice tuples

Dynamic Python Array Slicing

丶灬走出姿态 提交于 2019-12-07 17:52:11
问题 I am facing a situation where I have a VERY large numpy.ndarray (really, it's an hdf5 dataset) that I need to find a subset of quickly because they entire array cannot be held in memory. However, I also do not want to iterate through such an array (even declaring the built-in numpy iterator throws a MemoryError ) because my script would take literally days to run. As such, I'm faced with the situation of iterating through some dimensions of the array so that I can perform array-operations on

slicing behaviour question of a list of lists

落爺英雄遲暮 提交于 2019-12-07 15:07:05
问题 I got a function like def f(): ... ... return [list1, list2] this returns a list of lists [[list1.item1,list1.item2,...],[list2.item1,list2.item2,...]] now when I do the following: for i in range(0,2):print f()[i][0:10] it works and print the lists sliced but if i do print f()[0:2][0:10] then it prints the lists ignoring the [0:10] slicing. Is there any way to make the second form work or do I have to loop every time to get the desired result? 回答1: The reason why these two behave differently

pandas time series multiple slice

南笙酒味 提交于 2019-12-07 14:37:41
问题 I can see from the pandas documentation that you can go: df.loc[['a','b','c'],:] For time series, why can you not go: x = df.loc[['2005-10-27 14:30':'2005-10-27 15:15', '2006-04-14 14:40':'2006-04-14 15:20', '2008-01-25 14:30':'2008-01-25 15:30'],:] I get a syntax error. Can you not do multiple sliced ranges on a time series? Is there a workaround? 回答1: While a DataFrame index will accept a list of column indexes, it will not accept a list of row slice objects. This should do what you want,

Python code for sum with condition

强颜欢笑 提交于 2019-12-07 10:03:54
问题 The task is following: sum the list elements with even indexes and multiply the result by the last list's elemet. I have this oneliner solution code in Python. array = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41] print sum(i for i in array if array.index(i) % 2 == 0)*array[-1] if array != [] else 0 My result is -1476 ( The calculation is: 41*(-37-19+29+3-64+36+26+55+84-65) ) The right result is 1968. I can't figure it out why this code is not working correctly in this

Why are you unable convert Slice types?

耗尽温柔 提交于 2019-12-07 09:30:21
问题 I was wondering why you can't do: type Foo struct { A int } type Bar Foo foos := []Foo{Foo{1}, Foo{2}} bars := []Bar(foos) //cannot convert foos (type []Foo) to type []Bar and I found out that this would require the runtime to perform a loop over the slice to convert each of the elements, which would be non-idiomatic Go. This makes sense. However, could this not be solved by the compiler just aliasing Bar as Foo , so internally they're the same and they use the same type header underneath? I

How do I slice an array from an array of object literals?

别等时光非礼了梦想. 提交于 2019-12-07 06:43:14
问题 I have this array, in which each index contains an object literal. All of the object literals have the same properties. Some of the object literals have the same value for a given property, and I want to create a new array containing only those object literals. My idea is to sort the array, and slice it into a new array... Here is the array: var arr = []; arr[0] = { country: "United States", num: 27 }; arr[1] = { country: "Australia", num: 5 }; arr[2] = { country: "United States", num: 7 };

Python slice objects and __getitem__

泪湿孤枕 提交于 2019-12-07 06:27:22
问题 Is there something internal in python that treats arguments passed to __getitem_ _ differently, and automatically converts start:stop:step constructs into slices? Here's a demonstration of what i mean class ExampleClass(object): def __getitem__(self, *args): return args def __call__(self, *args): return args def randomMethod(self, *args): return args a = ExampleClass() #this works print a[3:7:2, 1:11:2] #syntax error on the first colon print a.randomMethod(3:7:2, 1:11:2) print a(3:7:2, 1:11:2

Numpy: arr[…,0,:] works. But how do I store the data contained in the slice command (…, 0, :)?

安稳与你 提交于 2019-12-07 05:15:06
问题 In Numpy (and Python in general, I suppose), how does one store a slice-index, such as (...,0,:), in order to pass it around and apply it to various arrays? It would be nice to, say, be able to pass a slice-index to and from functions. 回答1: Python creates special objects out of the slice syntax, but only inside the square brackets for indexing. You can either create those objects by hand (in this case, (...,0,:) is (Ellipsis, 0, slice(None, None, None)) , or you can create a little helper

How to create an array or a slice from an array unsafe.Pointer in golang?

心不动则不痛 提交于 2019-12-07 05:04:22
问题 A pointer to an array, let's say: p := uintptr(unsafe.Pointer(&array)) size := 5 I can't access to the variable array , the code above is used to make it more clear. Also, I know the size of the array, but the size is not constant, it changes according to the runtime. Now, I want to initialize a slice or an array with the known pointer, size and of course the data type. I come up with the following code: data := make([]byte, size) stepSize := unsafe.Sizeof(data[0]) for i := 0; i < size; i++ {