【发布时间】:2021-02-21 20:45:36
【问题描述】:
使用下面的代码来说明我的问题。
import numpy as np
np.random.seed(200)
a = np.array([1,21,6,41,8]) # given an array with 5 elements
idx = np.random.choice(5, 3, replace=False) # randomly select 3 indexes between 0 and 4
idx.sort() # sort indexes
print(idx) # [0 3 4]
print(a[idx]) # get random selected subset using the indexes, [ 1 41 8]
如何获取剩余索引[1,2]?
【问题讨论】:
-
您可以创建一个新列表
remaining = [x for x in range(5) if x not in idx]或从包含所有索引的列表中弹出随机索引
标签: python numpy random numpy-ndarray