python,如何在数组中提取相同元素的列索引,并将它们存储在新数组中,内部具有相同的数字
假设我有一个1D数组,我想在此数组中获取相同数字的所有列索引,然后将它们存储在单独的数组中。
,所有3的所有数字均在索引0,3,12中,对于索引4,5等。
x = np.array([3,6,8,3,1,1,5,8,5,0,2,0,3])
例如
a = np.array([0,3,12]) # Number 3
b = np.array([1]) # Number 6
c = np.array([2,7]) # Number 8
d = np.array([4,5]) # Number 1
e = np.array([9,11]) # Number 0
f = np.array([6,8]) # Number 5
g = np.array([10]) # Number 2
Let's say I have a 1D array and I want to obtain all the column indexes of the same numbers in this array and store them in separate arrays.
For example, all the numbers for 3 are in index 0,3,12, for 1 in index 4,5 etc.
x = np.array([3,6,8,3,1,1,5,8,5,0,2,0,3])
So the output would be
a = np.array([0,3,12]) # Number 3
b = np.array([1]) # Number 6
c = np.array([2,7]) # Number 8
d = np.array([4,5]) # Number 1
e = np.array([9,11]) # Number 0
f = np.array([6,8]) # Number 5
g = np.array([10]) # Number 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
输出:
编辑:
如果您想要一个1D数组,只需在输出上使用Flatten()方法,
大多数功能都可以在Numpy中搜索Numpy中的索引,该索引从下一次;););)
Outputs:
Edit:
if you want an 1D Array just use the flatten() method on the output
Most functions to search for indexes in numpy starts with arg for the next time ;)
生产:
编辑:
@jack的答案使用numpy内置功能,应是首选
produces:
Edit:
@jack's answer uses numpy built-in function and should be preferred
您可以将
numpy.unique
与return_inverse
和return_counts
flags这样You can use
numpy.unique
with thereturn_inverse
andreturn_counts
flags like so: