a = np.unique(A)
對於一維數組或者列表,unique函數去除其中重複的元素,並按元素由大到小返回一個新的無元素重複的元組或者列表
- import numpy as np
- A = [1, 2, 2, 5,3, 4, 3]
- a = np.unique(A)
- B= (1, 2, 2,5, 3, 4, 3)
- b= np.unique(B)
- C= ['fgfh','asd','fgfh','asdfds','wrh']
- c= np.unique(C)
- print(a)
- print(b)
- print(c)
-
-
-
c,s=np.unique(b,return_index=True)
return_index=True表示返回新列表元素在舊列表中的位置,並以列表形式儲存在s中。
- <span style="font-size:18px;">a, s= np.unique(A, return_index=True)
- print(a)
- print(s)
-
-
-
a, s,p = np.unique(A, return_index=True, return_inverse=True)
return_inverse=True 表示返回舊列表元素在新列表中的位置,並以列表形式儲存在p中
- <span style="font-size:18px;">a, s,p = np.unique(A, return_index=True, return_inverse=True)
- print(a)
- print(s)
- print(p)
-
-
-
-