Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从两个数组中找到匹配的元素和索引?_Python_Match_Intersection_Indices_Idl - Fatal编程技术网

Python 如何从两个数组中找到匹配的元素和索引?

Python 如何从两个数组中找到匹配的元素和索引?,python,match,intersection,indices,idl,Python,Match,Intersection,Indices,Idl,比如说, a = [1, 1, 2, 4, 4, 4, 5, 6, 7, 100] b = [1, 2, 2, 2, 2, 4, 5, 7, 8, 100] 我可以使用以下方法找到匹配的元素: np.intersect1d(a,b) 输出: array([ 1, 2, 4, 5, 7, 100]) intersect # array([ 1, 2, 4, 5, 7, 100]) ind_a # array([0, 2, 3, 6, 8, 9], dtyp

比如说,

a = [1, 1, 2, 4, 4, 4, 5, 6, 7, 100]
b = [1, 2, 2, 2, 2, 4, 5, 7, 8, 100]
我可以使用以下方法找到匹配的元素:

np.intersect1d(a,b)
输出:

array([  1,   2,   4,   5,   7, 100])
intersect
# array([  1,   2,   4,   5,   7, 100])
ind_a
# array([0, 2, 3, 6, 8, 9], dtype=int64)
ind_b
# array([0, 1, 5, 6, 7, 9], dtype=int64)
那么,如何分别获得数组
a
b
中匹配元素的索引

IDL中有一个函数作为“匹配”-


Python中有类似的函数吗?

您可以使用
枚举来跟踪索引,如下所示:

a = [1, 1, 2, 4, 4, 4, 5, 6, 7, 100]
b = [1, 2, 2, 2, 2, 4, 5, 7, 8, 100]
#    0  1  2  3  4  5  6  7  8   9

print([i for i,x in enumerate(zip(a,b)) if x[0] == x[1]])
这是怎么回事

我们正在利用惊人的
枚举
功能!此函数为iterable中的每个元素生成一个元组,第一个元素是枚举(或本例中的索引),第二个元素是iterable

下面是
zip(a,b)
的枚举形式

[(0, (1, 1)), (1, (1, 2)), (2, (2, 2)), (3, (4, 2)), (4, (4, 2)), (5, (4, 4)), (6, (5, 5)), (7, (6, 7)), (8, (7, 8)), (9, (100, 100))]

# lets look a little closer at one element
(0, (1, 1))
# ^     ^
# index iterable

从那以后就很简单了!解包iterable并检查两个元素是否相等,如果相等,则使用将枚举添加到列表中

numpy.intersect1d
中使用
返回索引:

intersect, ind_a, ind_b = np.intersect1d(a,b, return_indices=True)
输出:

array([  1,   2,   4,   5,   7, 100])
intersect
# array([  1,   2,   4,   5,   7, 100])
ind_a
# array([0, 2, 3, 6, 8, 9], dtype=int64)
ind_b
# array([0, 1, 5, 6, 7, 9], dtype=int64)
然后可以重复使用,如:

np.array(a)[ind_a]
np.array(b)[ind_b]

array([  1,   2,   4,   5,   7, 100])

使用
范围
如下:

matching_idxs = [idx for idx in range(len(a)) if a[idx] == b[idx]] 
print(matching_idxs)
# [0, 2, 5, 6, 9]

使用
枚举
邮政编码

a = [1, 1, 2, 4, 4, 4, 5, 6, 7, 100]
b = [1, 2, 2, 2, 2, 4, 5, 7, 8, 100]

output = [(i, x) for i, (x, y) in enumerate(zip(a, b)) if x == y]

print(output)


这导致元组列表为
(索引,值)

借助
zip
函数,这非常简单,可以并行循环两个列表:

>计数=0
>>>指数=[]
>>>对于拉链中的x,y(a,b):
如果x==y:
索引。追加(计数)
计数+=1
>>>指数
[0, 2, 5, 6, 9]

有时我们只是忽略了提供我们所需一切的文档:)