python - 我如何比较 python 中的两个列表,并返回第二个列表需要具有相同的值而不考虑顺序?

标签 python list comparison

a = [1, 2, 3, 4]
b = [2, 4, 3, 1]
c = [2, 3]

当比较 a 和 b 时,应该返回 True:a 中的所有项目都出现在 b 中, 中的所有项目b 出现在 a 中。

比较ac时,应该返回False:a中有不存在的项存在于 c 上。

pythonic 的方法是什么?

最佳答案

排序,然后比较。

sorted(a) == sorted(b)

关于python - 我如何比较 python 中的两个列表,并返回第二个列表需要具有相同的值而不考虑顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7539579/

相关文章:

python - CGI、python 和 setgid

c# - 排序 C++ vector 和 C# 列表

C# 遍历两个列表 - 可交换的内/外循环

python - 在 chrome 上找不到元素 ://settings/content

python - pymongo - 消息长度大于服务器最大消息大小

javascript - 文件比较脚本的逻辑

javascript - 比较不同编码的字符串

python - 检测和比较两个图像之间的形状

python - 如何从两侧找到最大数组

Java Spark/Velocity 模板/SQL2o