jingshen 发表于 2018-8-6 10:18:15

python -011-集合

s=frozenset()  s #frozenset()
  不可变 s.add(4)显示错误
  1、update(str)
  更新操作,str这个字符串拆分成多个字符,逐个添加进来
  s.update('world')
  s#set(['e', 'd', 'h', 'l', 'o', 'r', 'w'])
  2、赋值操作‘-=’
  s={'e', 'd', 'h', 'l', 'r', 'w'}
  s-=set('hehe')
  s2=set('hehe')#s2:{'h','e'}
  s=s-s2 #大集合减去小集合最后得到{'d', 'l', 'r', 'w'}
  3、删除一个集合
  del 删除的是变量 集合本身没有被删除
  4、成员关系运算符(in not in)
  set([' ', 'e', 'd', 'h', 'l', 'o', 'r', 'w'])
  print 'e' in s#True
  print 'y' in s#False
  5、集合等价
  s1=set('hello')
  s2=set('world')
  print s1==s2 #False
  print s1!=s2#True
  s2=set('hello')
  print s1==s2# True
页: [1]
查看完整版本: python -011-集合