Skip to main content
 首页 » 编程设计

python之有没有Python等同于MATLAB独有的

2024年10月24日9emanlee

我正在重写一段代码

nd = unique(nd,'rows'); 

The Mathworks 的文档

C = unique(A,'rows') treats each row of A as a single entity and returns the unique rows of A. The rows of the array C are in sorted order.

Python 中是否有类似的东西,或者我必须使用排序?

请您参考如下方法:

import numpy as np 
 
# Having an array: 
a = [1,2,2,3,4,4,3] 
print ("Original vector:") 
print(a) 
 
# Printting unique values of a: 
print ("Unique values of a:") 
print (np.unique(a)) 
 
# Other option: 
print  ("Another way:") 
print (set(a)) 

输出:

Original vector: 
[1, 2, 2, 3, 4, 4, 3] 
Unique values of a: 
[1 2 3 4] 
Another way: 
{1, 2, 3, 4}