Python can be written like this! Example.
Let's output the common element (AND), unique element (XOR), and union (OR) of set 1 and set 2 using the list comprehension notation.
set.py
#!/usr/bin/env python
#-*- coding:utf-8 *-*
set1 = [ u'craziness', u'Loud', u'Naked' ]
set2 = [ u'Naked', u'Loud', u'Void' ]
print 'SET1 = ' + ', '.join(set1)
print 'SET2 = ' + ', '.join(set2)
print ''
set_and = [ c for c in set1 if c in set2 ]
print 'AND = ' + ', '.join(set_and)
set_xor = []
set_xor.extend([ c for c in set1 if c not in set2 ])
set_xor.extend([ c for c in set2 if c not in set1 ])
print 'XOR = ' + ', '.join(set_xor)
set_or = []
set_or.extend(set_and)
set_or.extend(set_xor)
print 'OR = ' + ', '.join(set_or)
exit(None)
SET1 =craziness,Loud,Naked
SET2 =Naked,Loud,Void
AND =Loud,Naked
XOR =craziness,Void
OR =Loud,Naked,craziness,Void
Recommended Posts