Python Tutorial

Thursday, August 26, 2010

python set

'''
   This code use python set syntax
   - Set always holds unique data/object
'''
data=[7,1,2,3,1,4]
setA=set(data)
# Unique numbers of setA
print "setA: ",setA

data=[9,0,5,3,2,9]
setB=set(data)
# Unique numbers of setB
print "SetB: ",setB

'''
   All elements either in setA or setB
'''
setC=setA | setB
print setC

'''
   All elements both in setA and setB
'''
setC=setA & setB
print setC

'''
   All elements in setA or setB, but not both
'''
setC=setA ^ setB
print setC

'''
   All elements in setA but not in setB
'''
setC=setA - setB
print setC



Output:
setA:  set([1, 2, 3, 4, 7])
SetB:  set([0, 9, 2, 3, 5])
set([0, 1, 2, 3, 4, 5, 7, 9])
set([2, 3])
set([0, 1, 4, 5, 7, 9])
set([1, 4, 7])

0 comments:

Post a Comment