Python 查找数组中包含另一个数组中的一个值的索引
如何通过另一个具有多个“标记”的数组(标签)获取数组(a)中的值索引?例如,给定Python 查找数组中包含另一个数组中的一个值的索引,python,arrays,numpy,Python,Arrays,Numpy,如何通过另一个具有多个“标记”的数组(标签)获取数组(a)中的值索引?例如,给定 label = array([1, 2]) a = array([1, 1, 2, 2, 3, 3]) 目标是找到值为1或2的a;也就是说,0,1,2,3 我试过几种组合。以下这些似乎都不起作用 label = array([1, 2]) a = array([1, 1, 2, 2, 3, 3]) idx = where(a==label) # gives me only the index of the la
label = array([1, 2])
a = array([1, 1, 2, 2, 3, 3])
目标是找到值为1或2的a
;也就是说,0,1,2,3
我试过几种组合。以下这些似乎都不起作用
label = array([1, 2])
a = array([1, 1, 2, 2, 3, 3])
idx = where(a==label) # gives me only the index of the last value in label
idx = where(a==label[0] or label[1]) # Is confused by all or any?
idx = where(a==label[0] | label[1]) # gives me results as if nor. idx = [4,5]
idx = where(a==label[0] || label[1]) # syntax error
idx = where(a==bolean.or(label,0,1) # I know, this is not the correct form but I don`t remember it correctly but remember the error: also asks for a.all or a.any
idx = where(label[0] or label[1] in a) # gives me only the first appearance. index = 0. Also without where().
idx = where(a==label[0] or a==label[1]).all()) # syntax error
idx = where(a.any(0,label[0] or label[1])) # gives me only the first appearance. index=0. Also without where().
idx = where(a.any(0,label[0] | label[1])) # gives me only the first appearance. index=0. Also without where().
idx=where(a.any(0,label)) # Datatype not understood
好吧,我想你明白我的问题了。有人知道如何正确地做吗?最好是使用通用标签而不是标签[x]的解决方案,这样标签的使用在以后的更改中会更加多变。np。其中(a==label)
与np相同。非零(a==label)
。它告诉我们数组中所有非零(或True
)元素的坐标(索引),a==label
因此,与其尝试所有这些不同的where
表达式,不如关注条件数组
如果没有where
,下面是一些表达式产生的结果:
In [40]: a==label # 2 arrays don't match in size, scalar False
Out[40]: False
In [41]: a==label[0] # result is the size of a
Out[41]: array([ True, True, False, False, False, False], dtype=bool)
In [42]: a==label[0] or label[1] # or is a Python scalar operation
...
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
In [43]: a==label[0] | label[1]
Out[43]: array([False, False, False, False, True, True], dtype=bool)
最后一个与a==(标签[0]|标签[1])
相同,在=
之前对
求值
在了解在哪里提供了什么之前,您需要了解这些数组(或标量或错误)是如何生成的
2个相等性测试的正确组合(额外的()很重要):
使用广播分别测试标签
的两个元素。结果是二维阵列:
In [45]: a==label[:,None]
Out[45]:
array([[ True, True, False, False, False, False],
[False, False, True, True, False, False]], dtype=bool)
In [47]: (a==label[:,None]).any(axis=0)
Out[47]: array([ True, True, True, True, False, False], dtype=bool)
np.其中(a==label)
与np.非零(a==label)
相同。它告诉我们数组中所有非零(或True
)元素的坐标(索引),a==label
因此,与其尝试所有这些不同的where
表达式,不如关注条件数组
如果没有where
,下面是一些表达式产生的结果:
In [40]: a==label # 2 arrays don't match in size, scalar False
Out[40]: False
In [41]: a==label[0] # result is the size of a
Out[41]: array([ True, True, False, False, False, False], dtype=bool)
In [42]: a==label[0] or label[1] # or is a Python scalar operation
...
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
In [43]: a==label[0] | label[1]
Out[43]: array([False, False, False, False, True, True], dtype=bool)
最后一个与a==(标签[0]|标签[1])
相同,在=
之前对
求值
在了解在哪里提供了什么之前,您需要了解这些数组(或标量或错误)是如何生成的
2个相等性测试的正确组合(额外的()很重要):
使用广播分别测试标签
的两个元素。结果是二维阵列:
In [45]: a==label[:,None]
Out[45]:
array([[ True, True, False, False, False, False],
[False, False, True, True, False, False]], dtype=bool)
In [47]: (a==label[:,None]).any(axis=0)
Out[47]: array([ True, True, True, True, False, False], dtype=bool)
据我所知,您需要数组“a”中的1和2的索引
那样的话,试试看
label= [1,2]
a= [1,1,2,2,3,3]
idx_list = list()
for x in label:
for i in range(0,len(a)-1):
if a[i] == x:
idx_list.append(i)
据我所知,您需要数组“a”中的1和2的索引
那样的话,试试看
label= [1,2]
a= [1,1,2,2,3,3]
idx_list = list()
for x in label:
for i in range(0,len(a)-1):
if a[i] == x:
idx_list.append(i)
您可以使用:
上面返回一个掩码。如果需要索引,可以在掩码数组上调用numpy.nonzero
此外,如果标签
数组中的值是唯一的,则可以在一维中将假定_unique=True
传递到以可能加快速度。您可以使用:
上面返回一个掩码。如果需要索引,可以在掩码数组上调用numpy.nonzero
另外,如果label
数组中的值是唯一的,那么您可以将假定_unique=True
传递到inad
,以可能加快速度。我想我读到的是,您的目的是获取第一个列表“labels”中值的第二个列表“a”中的索引。我认为字典是存储这些信息的好方法,标签是键,索引是值
试试这个:
labels = [a,2]
a = [1,1,2,2,3,3]
results = {}
for label in labels:
results[label] = [i for i,x in enumerate(a) if x == label]
如果您想要1的索引,只需调用results[1]。列表理解是,枚举函数是这里真正的MVP。我想我读的是,您的目的是获取第一个列表“标签”中值的第二个列表“a”中的索引。我认为字典是存储这些信息的好方法,标签是键,索引是值
试试这个:
labels = [a,2]
a = [1,1,2,2,3,3]
results = {}
for label in labels:
results[label] = [i for i,x in enumerate(a) if x == label]
如果您想要1的索引,只需调用results[1]。列表理解是,枚举函数是这里真正的MVP。您需要更精确地描述您想要做的事情。就目前而言,您的问题非常模糊。不确定我是否理解您的意思,但请尝试以下操作:idx=where(a==label[0]| a==label[1])
将这些=
测试包装在()中,以获得正确的运算符顺序。@hpaulj哇!是的,这总是让我感到难受。你需要更准确地描述你想做什么。就目前而言,您的问题非常模糊。不确定我是否理解您的意思,但请尝试以下操作:idx=where(a==label[0]| a==label[1])
将这些=
测试包装在()中,以获得正确的运算符顺序。@hpaulj哇!是的,这总是让我心痛。亲爱的hpaulj,非常感谢你的深入回答。我在上面做了标记,因为其他人可能只是在这里快速回答。但对我来说,这是伟大的,因为我现在明白了问题的背景。干杯,谢谢你的深入回答。我在上面做了标记,因为其他人可能只是在这里快速回答。但对我来说,这是伟大的,因为我现在明白了问题的背景。干杯