• numpy.intersect1d( )函数


    numpy.intersect1d(ar1, ar2, assume_unique=False, return_indices=False)[source]
    Find the intersection of two arrays.   返回两个数组中共同的元素
    
    Return the sorted, unique values that are in both of the input arrays.  
    注意:是排序后的 Parameters: ar1, ar2 : array_like Input arrays. Will be flattened
    if not already 1D. assume_unique : bool If True, the input arrays are both assumed to be unique, which can speed up the calculation. Default is False. 默认是False,如果是True,假定输入的数组中元素
    return_indices : bool If True, the indices which correspond to the intersection of the two arrays are returned.
    The first instance of a value
    is used if there are multiple. Default is False. 默认是False,如果是True,返回共同元素的索引位置,因为返回共同元素是排序后的,所以索引位置是排序后的元素位置。
    如果共同元素在一个数组中有多次出现,只返回第一次出现的索引位置
    New
    in version 1.15.0. Returns: intersect1d : ndarray Sorted 1D array of common and unique elements. comm1 : ndarray The indices of the first occurrences of the common values in ar1. Only provided if return_indices is True. comm2 : ndarray The indices of the first occurrences of the common values in ar2. Only provided if return_indices is True.
    >>> np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1])
    array([1, 3])
    To intersect more than two arrays, use functools.reduce:
    
    >>>
    >>> from functools import reduce
    >>> reduce(np.intersect1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
    array([3])
    To return the indices of the values common to the input arrays along with the intersected values:
    
    >>>
    >>> x = np.array([1, 1, 2, 3, 4])
    >>> y = np.array([2, 1, 4, 6])
    >>> xy, x_ind, y_ind = np.intersect1d(x, y, return_indices=True)
    >>> x_ind, y_ind
    (array([0, 2, 4]), array([1, 0, 2]))
    >>> xy, x[x_ind], y[y_ind]
    (array([1, 2, 4]), array([1, 2, 4]), array([1, 2, 4]))
  • 相关阅读:
    Linux部署项目因为配置文件导致项目启动失败
    SQL Server数据库安装过程中遇到的问题
    jsp页面提交的时候,浏览器提示未响应,因为脚本长时间运行
    Jquery获取列表中的值和input单选、多选框控制选中与取消
    回调函数的理解历程
    类型转换之转String
    LigerUI开发过程中踩过的坑
    常用方法
    线段树维护区间平均值和方差
    线段树维护区间最大子段和
  • 原文地址:https://www.cnblogs.com/bravesunforever/p/12655706.html
Copyright © 2020-2023  润新知