可以使用 Python 中的 set 数据类型解决此问题。
首先,将两个数组转换为 set 类型,然后使用 == 操作符比较它们是否相等。如果它们是相等的,那么它们必须拥有相同的元素。
以下是一个示例代码:
array1 = [1, 2, 3]
array2 = [3, 2, 1]
set1 = set(array1)
set2 = set(array2)
if set1 == set2:
print("The arrays have the same elements!")
else:
print("The arrays do not have the same elements.")
输出:
The arrays have the same elements!
上一篇:比较2个数组并过滤匹配的元素。
下一篇:比较2个数组至少有3个相同元素