简体   繁体   English

如何通过 python 在另一个数组中找到具有相同值的索引?

[英]How to find index that has same value in another array by python?

I'm a beginner of Python.我是 Python 的初学者。 I would like to find indexes that have two largest values in ndarray.我想在 ndarray 中找到具有两个最大值的索引。 For example, ndarray x like this.例如,ndarray x 像这样。

x = np.array([2,3,5,3,7,3,1,5])

Because the two largest values are 7 and 5. The answer should be因为两个最大值是 7 和 5。答案应该是

ind = [2,4,7]
x[ind] = [5,7,5]

Would you tell me how to code it?你能告诉我如何编码吗?

You can accomplish this in 3 steps:您可以通过 3 个步骤完成此操作:

  1. Sort your unique array values (np.unique also sorts) np.unique(…)对您的唯一数组值进行排序(np.unique 也排序) np.unique(…)
  2. Slice the last N values (the maximums) from the sorted unique array …[-max_n:]从排序的唯一数组中切片最后 N 个值(最大值) …[-max_n:]
  3. Find the indices where your array has those maximums via np.where(np.isin(…))通过np.where(np.isin(…))查找数组具有这些最大值的索引
import numpy as np

max_n = 2
x = np.array([2,3,5,3,7,3,1,5])
max_values = np.unique(x)[-max_n:]
max_indices = np.where(np.isin(x, max_values))[0]

print(
    f'{max_indices    = }',
    f'{x[max_indices] = }',
    sep='\n'
)
largest_indices    = array([2, 4, 7])
x[largest_indices] = array([5, 7, 5])

You can use np.where selector.您可以使用np.where选择器。 First you extract indices of the maximum value, then you work identically on a subset of the original array without this last value(s)首先提取最大值的索引,然后在没有最后一个值的原始数组的子集上进行相同的工作

x = np.array([2,3,5,3,7,3,1,5])
largest_indices = np.where(x == np.max(x))[0]
# In one line you select the subarray without the largest value
largest2_indices = np.where(x == np.max(x[x < np.max(x)]))[0]
ind = np.sort(np.concatenate((largest_indices, largest2_indices)))

Or, in one single, confusing, line:或者,在一个令人困惑的行中:

ind = np.sort(np.where((x == np.max(x)) | (x == np.max(x[x < np.max(x)])))[0])

The result:结果:

print(ind)
>>> [2 4 7]
print(x[ind])
>>> [5 7 5]

Theres several ways to accomplish this.有几种方法可以做到这一点。 One simple way is to一种简单的方法是

  1. Sort the array对数组进行排序
sorted=np.argsort(x)

2.select the largest 2 values 2.select最大2个值

max=x[-1]
second
for s in reversed(sorted):
   if s !=max:
     second=s
  1. Find the indexs with those values查找具有这些值的索引
  return np.where(x==max or x==second)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python查找数据框中具有最大值的数组的索引? - How to find index of an array which has maximum value in dataframe using python? 如何在另一个数组中找到一个数组对应的索引? (Python) - How to find the index corresponding to an array in another array? (python) 一次 python 如何在数组的多个索引中设置相同的值? - How to set the same value in multiple index of array once python? 如何在python中将数组的索引值分配给其他数组相同的索引值 - how to assign index value of an array to the others array same index value in python 如何在Python中的二维数组中找到值的索引? - How to find the index of a value in 2d array in Python? 如何使用python将具有相同值的所有元素收集到数组中? - how to collect all elements that has same value into array with python? 如何用另一个数组的相同索引中的值替换一个数组中的值? - How can I replace a value from one array with a value in the same index of another array? 用另一个数组的相同索引的值替换一个数组中的值? - Replacing a value from one array with a value of the same index of another array? 在 python 数组中找到接近数字的值并对其进行索引 - Find value close to number in python array and index it 如何通过一个数组值来查找数组的索引? - How to find the index of array by a value which is an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM