python中set 函式的用法

2021-07-25 15:13:54 字數 675 閱讀 9457

python set類是在python的sets模組中,大家現在使用的python2.3中,不需要匯入sets模組可以直接建立集合。

>>>set('boy')

set(['y', 'b', 'o'])

集合的新增有兩種常用方法,分別是add和update。

集合add方法:是把要傳入的元素做為乙個整個新增到集合中,例如:

>>> a = set('boy')

>>> a.add('python')

>>> a

set(['y', 'python', 'b', 'o'])

集合update方法:是把要傳入的元素拆分,做為個體傳入到集合中,例如:

>>> a = set('boy')

>>> a.update('python')

>>> a

set(['b', 'h', 'o', 'n', 'p', 't', 'y'])

集合刪除操作方法:remove

set(['y', 'python', 'b', 'o'])

>>> a.remove('python')

>>> a

set(['y', 'b', 'o'])

集合的交集、合集(並集)、差集,了解集合set的這些非常好用的功能前,要先了解一些集合操作符號

簡單的演示下差集、交集和合集的概念:

python中set 函式的用法

python set類是在python的sets模組中,大家現在使用的python2.3中,不需要匯入sets模組可以直接建立集合。set boy set y b o 集合新增 刪除 集合的新增有兩種常用方法,分別是add和update。集合add方法 是把要傳入的元素做為乙個整個新增到集合中,例如...

python中set的用法

s1 s2 print s1.difference s2 差集 s1有s2沒有的 print s1.symmetric difference s2 差集 去除共有的 print s1.difference update s2 更新s1 去掉和s2相同的值 print s1.symmetric dif...

Matlab中的set函式用法

屬性的操作 set函式的呼叫格式為 set 控制代碼,屬性名1,屬性值1,屬性名2,屬性值2,例1 x 0 0.2 12 y1 bessel 1,x y2 bessel 2,x y3 bessel 3,x h plot x,y1,x,y2,x,y3 set h,set h,set h,linewid...