简体   繁体   English

从python中的列表子集进行布尔索引

[英]boolean indexing from a subset of a list in python

I have an array of names, along with a corresponding array of data. 我有一个名称数组,以及一个对应的数据数组。 From the array of names, there is also a smaller subset of names: 在名称数组中,还有一个较小的名称子集:

data = np.array([75., 49., 80., 87., 99.])
arr1 = np.array(['Bob', 'Joe', 'Mary', 'Ellen', 'Dick'], dtype='|S5')
arr2 = np.array(['Mary', 'Dick'], dtype='|S5')

I am trying to make a new array of data corresponding only to the names that appear in arr2. 我试图做一个新的数据数组,仅对应于出现在arr2中的名称。 This is what I have been able to come up with on my own: 这是我自己能想到的:

TF = []
for i in arr1: 
    if i in arr2:
        TF.append(True)
    else:
        TF.append(False)
new_data = data[TF]

Is there a more efficient way of doing this that doesn't involve a for loop? 有没有一种不涉及for循环的更有效的方法? I should mention that the arrays themselves are being input from an external file, and there are actually multiple arrays of data, so I can't really change anything about that. 我应该提到的是,数组本身是从外部文件输入的,实际上有多个数据数组,因此我无法对此进行任何更改。

You can use numpy.in1d , which tests whether each element in one array is also present in the second array. 您可以使用numpy.in1d ,它测试一个数组中的每个元素是否也出现在第二个数组中。

Demo 演示版

>>> new_data = data[np.in1d(arr1, arr2)]
>>> new_data
array([ 80.,  99.])

in1d returns an ndarray of bools, which is analogous to the list you constructed in your original code: in1d返回一个ndarray ,类似于您在原始代码中构造的列表:

>>> np.in1d(arr1, arr2)
array([False, False,  True, False,  True], dtype=bool)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM