我有两份清单:
from secrets import token_urlsafe
a = [token_urlsafe() for i in range(100)]
b = a[50:]
我需要找出这两个列表之间的区别。我是使用集合理解还是集合差异
Python文档描述了实现这一点的两种方法
set(a) - set(b)
Return a new set with elements in the set that are not in the others
{i for i in a if i not in b}
Use a set comprehension:
{c for c in 'abracadabra' if c not in 'abc'}
我们可以看到,使用timeit时,集合差分方法的速度更快:
请注意,时间可能因集合a的长度、集合b的长度、公共元素的数量以及它们的公共位置而不同
相关问题 更多 >
编程相关推荐