更新时间:2023-11-06 15:01:04
有很大的不同.
__contains__
(in
运算符)的集合比列表更有效.set(([1],[2]))
,您将得到一个TypeError
.__contains__
(in
operator) a lot more efficient for sets than lists.set(([1],[2]))
you'll get a TypeError
.在实际应用中,列表很容易排序和排序,而当您不想重复且不关心顺序时,可以很好地使用集合.
In practical applications, lists are very nice to sort and have order while sets are nice to use when you don't want duplicates and don't care about order.
还请注意,如果您不关心订单等,可以使用
new_set = myset.intersection(mylist)
以获得set
和list
之间的交集.
to get the intersection between a set
and a list
.