Set
Set
  • A set object is an unordered collection of immutable values, set type is mutable
  • #!/usr/bin/python
    
    s = set(['a', 'a', 'b', 'c', 'd']); #['a', 'b', 'c', 'd']
    s2 = set(['a', 'd', 'e', 'f']);
    
    #access element
    for e in s:
        print e,
    print
    
    print s|s2; #union
    print s&s2; #intersection
    print s-s2; #difference
    
    s.add('g'); #['a', 'b', 'c', 'd', 'g']
    s.remove('d'); #remove element; raise KeyError if not present
    s.discard('c'); #remove if present
    s.pop(); #remvoe an arbitrary element
    if s.issuperset(set(['b'])):
        print '[b, g] is the super set of [b]'
    s.clear(); #remove all element in set
    		
    Frozenset
  • Frozenset type is immutable
  • #!/usr/bin/python
    
    s = frozenset(['a', 'a', 'b', 'c', 'd']); #['a', 'b', 'c', 'd']
    s2 = frozenset(['a', 'd', 'e', 'f']);
    
    print s|s2; #union
    print s&s2; #intersection
    print s-s2; #difference
    
    if s.issuperset(frozenset(['b'])):
        print '[a, b, c, d] is the super set of [b]'
    		
    Reference
  • Standard Library