Python 中交、并、补、差集的实现都依赖于集合(set)这种元素类型,如下 x
和 y
两种定义方式是等价的。
x = {1,2,3,4}
y = set([3,3,4,5])
print("x = %s, y = %s, type(x) = %s, type(y) = %s, x&y = %s, x|y = %s, x^y = %s, x-y = %s, y-x = %s" %(x, y, type(x), type(y), x&y, x|y, x^y, x-y, y-x) )
# 输出如下:
# x = {1, 2, 3, 4}, y = {3, 4, 5}, type(x) = <class 'set'>, type(y) = <class 'set'>, x&y = {3, 4}, x|y = {1, 2, 3, 4, 5}, x^y = {1, 2, 5}, x-y = {1, 2}, y-x = {5}
猜你喜欢:👇🏻
⭐【Python】可遍历的数据类型有哪些?
⭐【Python】创建长度为n的全0列表和全1列表
⭐【Python】与或非的符号表示