set()中的remove和discard

set中使用remove和discard都删除元素,用remove删除时如果元素在集合中没有会报错,而discard不会

c = set({'1','2','3'})
c
{'1', '2', '3'}

c.remove('1')
c
{'2', '3'}

c.remove('4')
KeyError: '4'

 对比discard

c = set({'1','2','3'})
c
{'1', '2', '3'}

c.discard('1')
c

{'2', '3'}

c.discard('4')
c
{'2', '3'}

  

原文地址:https://www.cnblogs.com/xiaodongsuibi/p/12163210.html