107:2つの配列の差を取る
http://pleac.sourceforge.net/pleac_python/arrays.html
>>> a = (1, 3, 5, 6, 7, 8) >>> b = (2, 3, 5, 7, 9) >>> aonly = [item for item in a if item not in b] >>> aonly [1, 6, 8]
python2.3から実装された setsモジュール を使います。
set型からリストへの変換はlist関数を使います。
>>> import sets >>> a = (1, 3, 5, 6, 7, 8) >>> b = (2, 3, 5, 7, 9) >>> a_set=sets.Set(a) >>> b_set=sets.Set(b) 差をとる >>> aonly=list(a_set - b_set) >>> aonly [8, 1, 6] 足す >>> union=a_set | b_set >>> union Set([1, 2, 3, 5, 6, 7, 8, 9]) >>> list(union) [1, 2, 3, 5, 6, 7, 8, 9] だぶりを抜き出す >>> isec=a_set & b_set >>> isec Set([3, 5, 7]) 違いを取り出す >>> diff=a_set ^ b_set >>> diff Set([8, 1, 2, 6, 9])