Home  >  Article  >  Backend Development  >  How to compare the differences between array elements in Python

How to compare the differences between array elements in Python

巴扎黑
巴扎黑Original
2017-08-18 13:35:082935browse

The following editor will bring you a Python method to compare the similarities and differences of the elements of two arrays. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

Get the intersection/union/difference of two arrays through set():


print set(a).intersection(set(b)) # 交集
print set(a).union(set(b)) # 并集
print set(a).difference(set(b)) # 差集,在a中但不在b中的元素
print set(b).difference(set(a)) # 差集,在b中但不在a中的元素

The above is the detailed content of How to compare the differences between array elements in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn