Python学習入門(9)——set操作

6003 ワード

pythonのsetは他の言語と類似しており、無秩序で重複しない要素セットであり、基本的な機能は関係テストと重複要素の除去を含む.コレクションオブジェクトはunion(連合)、intersection(交差)、difference(差)、sysmmetric_もサポートします.difference(対称差セット)などの数学演算.setsはx in set,len(set),for x in setをサポートし,無秩序な集合としてsetsは要素位置や挿入点を記録しない.したがってsetsはindexing,slicing,または他のクラスシーケンス(sequence-like)の操作をサポートしません.
#!/usr/bin/env python

# it-homer in 2013

import sys

reload(sys)
sys.setdefaultencoding('utf-8')

def test_set():
  x = set('ithomer')
  y = set(['i', 't', 'h', 's', 'n'])
  print("%s, %s" % (x, y))      # set(['e', 'i', 'h', 'm', 'o', 'r', 't']), set(['i', 'h', 's', 't', 'n'])

  #   
  print("%s" % (x&y))           # set(['i', 'h', 't'])
  f = x.intersection(y)
  print(f)                      # set(['i', 'h', 't'])

  #   
  print("%s" % (x|y))           # set(['e', 'i', 'h', 'm', 'o', 'n', 's', 'r', 't'])
  f = x.union(y)
  print(f)                      # set(['e', 'i', 'h', 'm', 'o', 'n', 's', 'r', 't'])

  #   
  print("%s" % (x-y))           # set(['m', 'r', 'e', 'o'])
  f = x.difference(y)
  print(f)                      # set(['m', 'r', 'e', 'o'])

  print("%s" % (y-x))           # set(['s', 'n'])
  f = y.difference(x)
  print(f)                      # set(['s', 'n'])

  #     (  x y ,           ) 
  print("%s" %(x^y))            # set(['e', 'm', 'o', 'n', 's', 'r'])
  print("%s" %(y^x))            # set(['s', 'r', 'e', 'm', 'o', 'n'])

  f = x.symmetric_difference(y) # set(['e', 'm', 'o', 'n', 's', 'r'])
  print(f)
  f = y.symmetric_difference(x) # set(['s', 'r', 'e', 'm', 'o', 'n'])
  print(f)


  a = [11, 22, 33, 44, 11, 33, 22]
  b = set(a)
  print(b)              # set([33, 11, 44, 22])

  for i in b:
    print i,            #       : 33 11 44 22



def test_set2():
  s = set([1, 3, 5, 7, 9])
  t = set("ithomer")
  
  print(t)              # set(['e', 'i', 'h', 'm', 'o', 'r', 't'])

  #     
  print("%s" % (s|t))   # set([1, 3, 'e', 5, 7, 'i', 9, 'm', 'o', 'r', 't', 'h'])

  #   
  print("%s" % (t&s))   # set([])

  #   
  print("%s" % (t-s))   # set(['e', 'i', 'h', 'm', 'o', 'r', 't'])
  print("%s" % (s-t))   # set([7, 1, 3, 5, 9])

  #     
  print("%s" % (t^s))   # set([1, 3, 'e', 5, 7, 'i', 9, 'm', 'o', 'r', 't', 'h'])


  print(t)              # set(['e', 'i', 'h', 'm', 'o', 'r', 't'])
  t.add('y')
  t.add('g')
  print(t)              # set(['e', 'g', 'i', 'h', 'm', 'o', 'r', 't', 'y'])


  try:
    print(len(t))       # 9
    t.remove('Y')
  except Exception, e:
    print e             # 'Y'

  print(len(t))         # 9
  t.remove('y')
  print(len(t))         # 8



def test_set3():
  s = set('ithomer')
  t = set('home')

  #    x     s    
  f = 'i' in s
  print(f)              # True

  #    x      s    
  f = 'i' not in s
  print(f)              # False

  #      t           s   
  f = t.issubset(s)
  print(f)              # True

  #      t           s  
  f = s.issuperset(t)
  print(f)              # True

  #      
  print(s)              # set(['e', 'i', 'h', 'm', 'o', 'r', 't'])
  f = s.copy()
  print(f)              # set(['r', 'e', 't', 'i', 'h', 'm', 'o'])
  
  f = hash('i')
  print(f)


def test_set4():
  s = set('ithomer')
  t = set('home123')

  # s |= t
  s.update(t)       
  print(s)              # set(['e', 'i', 'h', 'm', 'o', '1', '3', 'r', 't', '2'])


  s = set('ithomer')
  # s &= t
  s.intersection_update(t)
  print(s)              # set(['h', 'e', 'm', 'o'])

  
  s = set('ithomer')
  # s -= t
  s.difference_update(t)
  print(s)              # set(['i', 'r', 't'])


  s = set('ithomer')
  # s ^= t
  s.symmetric_difference_update(t)
  print(s)              # set(['i', '1', '3', 'r', 't', '2'])


  s = set('ithomer')
  print(s)              # set(['e', 'i', 'h', 'm', 'o', 'r', 't'])
  s.add('I')
  print(s)              # set(['e', 'i', 'h', 'm', 'o', 'I', 'r', 't'])
  s.remove('i')          
  print(s)              # set(['e', 'h', 'm', 'o', 'I', 'r', 't'])
  s.discard('I')
  print(s)              # set(['e', 'h', 'm', 'o', 'r', 't'])
  s.pop()
  print(s)              # set(['h', 'm', 'o', 'r', 't'])
  s.clear()
  print(s)              # set([])



if __name__ == "__main__":
#  test_set()
#  test_set2()
  test_set3()
#  test_set4()