集合(set)

集合对象是一组无序排列的可哈希的值。集合成员可以做字典的键。

a = set('abce')
b = set('abcd')
c = frozenset('abc')
a.add('f')
a.update('g')
print 'a:',a
print 'b:',b
#c.update('d') c is const set,can not be changed

print 'a|b:',a|b #all elements in a or b
print 'a|b:',a&b #all elements in a and b
print 'a-b:',a-b #all elements in a but not in b
print 'a^b:',a^b #all elements in b but not in a,or in a but not in b


a: set(['a', 'c', 'b', 'e', 'g', 'f'])
b: set(['a', 'c', 'b', 'd'])
a|b: set(['a', 'c', 'b', 'e', 'd', 'g', 'f'])
a|b: set(['a', 'c', 'b'])
a-b: set(['e', 'g', 'f'])
a^b: set(['e', 'd', 'g', 'f'])

你可能感兴趣的:(set)