1 条题解

  • 0
    @ 2026-3-26 13:20:39

    NUMPY065 题解:找相同元素位置

    题目

    找出两个数组中相同元素的索引。

    解题思路

    1. 读取两个数组
    2. 使用 np.where(Z1 == Z2)\texttt{np.where(Z1 == Z2)} 找出相同元素的位置

    代码

    import numpy as np
    
    n = int(input())
    Z1 = np.array(list(map(int, input().split())))
    Z2 = np.array(list(map(int, input().split())))
    result = np.where(Z1 == Z2)
    print(result[0])
    

    代码详解

    • Z1 == Z2\texttt{Z1 == Z2} —— 比较两个数组,返回布尔数组
    • np.where\texttt{np.where} —— 返回满足条件的索引
    • result[0]\texttt{result[0]} —— np.where\texttt{np.where} 返回元组,取第一个元素
    • 1

    信息

    ID
    338
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    (无)
    递交数
    0
    已通过
    0
    上传者