python set

1.
a = set('abcdabcd')
b = set('abcd')
print(a)
print(b)
print(a == b)
print(a < b)
print(a <= b)
print(a >= b)
print(a.issubset(b)) #相当于print(a <= b)
print(a.issuperset(b)) #相当于print(a >= b)

输出:

{'d', 'a', 'b', 'c'}
{'d', 'a', 'b', 'c'}
True
False
True
True
True
True



2.
a = set('abc')
b = set('bcd')
print(a | b)
print(a.union(b))

print(a.intersection(b))
print(a & b)
a.intersection_update(b)
print(a)

a = set('abc')
print(a.difference(b))
print(a - b)
print(b - a)
a.difference_update(b)
print(a)

a = set('abc')
print(a.symmetric_difference(b))
a.symmetric_difference_update(b)
print(a)

输出:

{'d', 'a', 'c', 'b'}
{'d', 'a', 'c', 'b'}
{'c', 'b'}
{'c', 'b'}
{'c', 'b'}
{'a'}
{'a'}
{'d'}
{'a'}
{'d', 'a'}
{'d', 'a'}

 3.

a = set('abc')
b = set('bcd')

print(a ^ b) #等价于print(a.symmetric_difference(b))

输出:

{'d', 'a'}

posted on 2017-09-19 13:54  靖康耻  阅读(108)  评论(0编辑  收藏  举报

导航