array-indexing

numpy indexing: shouldn't trailing Ellipsis be redundant?

耗尽温柔 提交于 2019-12-31 04:56:07
问题 While trying to properly understand numpy indexing rules I stumbled across the following. I used to think that a trailing Ellipsis in an index does nothing. Trivial isn't it? Except, it's not actually true: Python 3.5.2 (default, Nov 11 2016, 04:18:53) [GCC 4.8.5] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import numpy as np >>> >>> D2 = np.arange(4).reshape((2, 2)) >>> >>> D2[[1, 0]].shape; D2[[1, 0], ...].shape (2, 2) (2, 2) >>> D2[:, [1, 0]].shape;

Using python range objects to index into numpy arrays

前提是你 提交于 2019-12-12 18:16:43
问题 I've seen it once or twice before, but I can't seem to find any official docs on it: Using python range objects as indices in numpy. import numpy as np a = np.arange(9).reshape(3,3) a[range(3), range(2,-1,-1)] # array([2, 4, 6]) Let's trigger an index error just to confirm that ranges are not in the official range (pun intended) of legal indexing methods: a['x'] # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # IndexError: only integers, slices (`:`), ellipsis (`...

numpy indexing: shouldn't trailing Ellipsis be redundant?

喜夏-厌秋 提交于 2019-12-02 06:23:21
While trying to properly understand numpy indexing rules I stumbled across the following. I used to think that a trailing Ellipsis in an index does nothing. Trivial isn't it? Except, it's not actually true: Python 3.5.2 (default, Nov 11 2016, 04:18:53) [GCC 4.8.5] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import numpy as np >>> >>> D2 = np.arange(4).reshape((2, 2)) >>> >>> D2[[1, 0]].shape; D2[[1, 0], ...].shape (2, 2) (2, 2) >>> D2[:, [1, 0]].shape; D2[:, [1, 0], ...].shape (2, 2) (2, 2) >>> # so far so expected; now ... >>> D2[[[1, 0]]].shape; D2[[[1

Do pointers support “array style indexing”?

喜夏-厌秋 提交于 2019-11-27 03:40:56
问题 (Self-answered Q&A - this matter keeps popping up) I assume that the reader is aware of how pointer arithmetic works. int arr[3] = {1,2,3}; int* ptr = arr; ... *(ptr + i) = value; Teachers/C books keep telling me I shouldn't use *(ptr + i) like in the above example, because "pointers support array style indexing" and I should be using ptr[i] = value; instead. No argument there - much easier to read. But looking through the C standard, I find nothing called "array style indexing". In fact, the