Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
640 views
in Technique[技术] by (71.8m points)

python - Index multiple, non-adjacent ranges in numpy

I'd like to select multiple, non-adjacent ranges from a 1d numpy array (or vector).

Suppose:

>>> idx = np.random.randint(100, size=10)
array([82,  9, 11, 94, 31, 87, 43, 77, 49, 50])

This works, of course:

>>> idx[0:3]
array([82,  9, 11])

And this works to fetch via individual indices:

>>> idx[[0,3,4]]
array([82, 94, 31])

But what if I want to select the ranges 0:3, and 7:?

I've tried:

>>> idx[[0:3,7:]]
SyntaxError: invalid syntax

Is there a simple way to do this, or do I need to generate them separately and concatenate?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to concatenate, either before or after indexing. np.r_ makes it easy

In [116]: idx=np.array([82,  9, 11, 94, 31, 87, 43, 77, 49, 50])
In [117]: np.r_[0:3,7:10]
Out[117]: array([0, 1, 2, 7, 8, 9])
In [118]: idx[np.r_[0:3,7:10]]
Out[118]: array([82,  9, 11, 77, 49, 50])

np.r_ expands the slices and concatenates them.

You can mix slices and lists:

In [120]: np.r_[0:3,7:10,[0,3,4]]
Out[120]: array([0, 1, 2, 7, 8, 9, 0, 3, 4])

Concatenating before indexing is probably faster than after, but for 1d array like this, I don't think the difference is significant.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...