gpt4 book ai didi

python - 使用 .all() 和 any() 获取搜索数组的索引

转载 作者:太空宇宙 更新时间:2023-11-03 15:46:15 28 4
gpt4 key购买 nike

我有数组列表,我正在使用 .all() 和 .any() 来检查正在搜索的数组是否在数组列表中。

t = np.array([[1,2,3], [4,5,6] , [7,8,9], [10, 11, 12]])

t2 = np.array([1,2,3])

print((t == t2).all(1))
outpu:> [ True False False False]

这里我可以检查t2是否在t中

print((t == t2).all(1).any())
outpu:> True

我的问题是,我可以使用 .all 和 .any 方法获取 t2 中存在的哪个索引吗?

最佳答案

您正在寻找 np.flatnonzeronp.where

>>> np.flatnonzero((t == t2).all(1))

或者,

>>> np.where((t == t2).all(1))[0]

array([0])

关于python - 使用 .all() 和 any() 获取搜索数组的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49792966/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com