python set

set

   set有去重和排序功能

>>> a=[1,2,3,3,3,4,5,1,6,7]

>>> b=set(a)

>>> type(b)

<type 'set'>

>>> print b

set([1, 2, 3, 4, 5, 6, 7])


>>> x = set('spam')  

>>> y = set(['h','a','m'])  

>>> x, y  

(set(['a', 'p', 's', 'm']), set(['a', 'h', 'm']))

>>> x & y # 交集  

set(['a', 'm'])  


>>> x | y # 並集  

set(['a', 'p', 's', 'h', 'm'])  


>>> x - y # 差集  

set(['p', 's'])




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章