去除指定列表/数组中的重复元素 返回一个无重复元素的列表/数组。
实例A [1,2,2,4,1,6] a unique(A) a,b,c unique(A,return_index True,return_inverse True) a,b,c (array([1, 2, 4, 6]), array([0, 1, 3, 5], dtype int64), array([0, 1, 1, 2, 0, 3], dtype int64))参数说明 A 表示函数作用的对象 为数组/列表。return_index:有True和False两种取值 True表示返回处理后列表或者数组的各个元素在原列表/数组中的第一个索引位置 False表示不返回return_reverse 仅列表对象有 :有True和False两种取值 True表示返回处理后列表/数组各个元素在原列表/数组中的所有索引位置。 常见错误调用
1.设定的参数与返回变量数不匹配造成的错误
import numpy as np A [1,2,2,4,1,6] a np.unique(A) a,b,c np.unique(A,return_index True,return_inverse False) a,b,c not enough values to unpack (expected 3, got 2)
import numpy as np A [1,2,2,4,1,6] a np.unique(A) a,b np.unique(A,return_index True,return_inverse False) (array([1, 2, 4, 6]), array([0, 1, 3, 5], dtype int64))
2.对数组调用unique()时仍然认为有return_reverse参数
import numpy as np B np.random.randint(0,5,8) # print(B) b,c,d np.unique(B,return_index True,return_reverse True) b,c,d _unique_dispatcher() got an unexpected keyword argument return_reverse
import numpy as np B np.random.randint(0,5,8) b,c np.unique(B,return_index True) [2 4 2 0 0 4 0 4] (array([0, 2, 4]), array([3, 0, 1], dtype int64))



