社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

比较两个矢量python[duplicate]

Dumitru Galbura • 5 年前 • 1495 次点击  

如果我有两个向量 a b ,

a = [1, 3, 6]
b = [3, 1, 6]

由于向量的内容是相同的,是否有可能以某种方式进行比较,从而得到正确的结果?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40614
 
1495 次点击  
文章 [ 3 ]  |  最新文章 5 年前
Rohit-Pandey
Reply   •   1 楼
Rohit-Pandey    6 年前

试试这个:

 set(a) == set(b)

因为 set 自动排序。

Sheldore David Zwicker
Reply   •   2 楼
Sheldore David Zwicker    6 年前

你可以使用 sorted 然后比较。正如 blhsing ,这是 O(n log n) 操作而解决方案 Counter o(n) . 自从 n=3 在您的情况下,差异可以忽略不计,但差异将变得明显 n . 你可能有兴趣知道这个。

a = [1, 3, 6] 
b = [3, 1, 6]
sorted(a) == sorted(b)
# True

Here 你会发现关于这个话题的广泛讨论。

blhsing
Reply   •   3 楼
blhsing    6 年前

你可以使用 collections.Counter :

from collections import Counter
Counter(a) == Counter(b)