千巷猫影
您可以使用np.repeat和np.concatenate。>>> import numpy as np>>> >>> class By_Row:... def __getitem__(self, idx):... y, *x = (np.arange(i.start, i.stop, i.step) for i in idx)... return y.repeat(np.fromiter((i.size for i in x), int, y.size)), np.concatenate(x)... >>> >>> b_ = By_Row()>>> >>> A = sum(np.ogrid[:600:100, :12])>>> Aarray([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111], [200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211], [300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311], [400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411], [500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511]])>>> A[b_[2:5, 2:4, 3:7, 0:11]]array([202, 203, 303, 304, 305, 306, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410])
Cats萌萌
这是您可以做到的一种方法:x = range(2,5)y = range(17)divs = [(2,4), (3,7), (12,17)]y_vals = []x_vals = []for d, div in enumerate(divs): y_grp = y[div[0]:div[1]] y_vals += y_grp x_vals += [x[d]]*len(y_grp)print(x_vals)print(y_vals)> [2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4]> [2, 3, 3, 4, 5, 6, 12, 13, 14, 15, 16]