淺談python中set使用
淺談python中set使用
In [2]: a = set() # 常用操作1 In [3]: a Out[3]: set() In [4]: type(a) Out[4]: set In [5]: b = set([1, 3]) In [6]: b Out[6]: {1, 3} In [7]: type(b) Out[7]: set In [8]: b.update(2) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-8-d51e2fe4c50a> in <module>() ----> 1 b.update(2) TypeError: 'int' object is not iterable In [9]: b.update({2}) In [10]: b Out[10]: {1, 2, 3} In [11]: b.update([4]) In [12]: b Out[12]: {1, 2, 3, 4} In [13]: a.di a.difference a.difference_update a.discard In [13]: a.dif a.difference a.difference_update In [13]: a.difference(b) Out[13]: set() In [14]: a Out[14]: set() In [15]: b.difference(a) Out[15]: {1, 2, 3, 4} In [16]:
常用操作2
In [16]: a.add({1, 3}) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-16-98cdf4d0875e> in <module>() ----> 1 a.add({1, 3}) TypeError: unhashable type: 'set' In [17]: a.add(4) In [18]: a Out[18]: {4} In [19]: a.issu a.issubset a.issuperset In [19]: a.issubset(b) Out[19]: True In [20]: a.remove(4) In [21]: a Out[21]: set() In [22]: a.union(b) Out[22]: {1, 2, 3, 4} In [23]: a Out[23]: set() In [24]: b Out[24]: {1, 2, 3, 4} In [25]: b.pop() Out[25]: 1 In [26]: a.copy(b) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-26-9e8a5f057ffd> in <module>() ----> 1 a.copy(b) TypeError: copy() takes no arguments (1 given) In [27]: a.copy() Out[27]: set() In [28]: c = a.copy() In [29]: c Out[29]: set() In [30]: a Out[30]: set() In [31]: a.add({234}) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-31-6073e02d68a9> in <module>() ----> 1 a.add({234}) TypeError: unhashable type: 'set' In [32]: a.add(234) In [33]: c Out[33]: set() In [34]: a Out[34]: {234}
常用操作3
In [35]: a.clear() In [36]: a Out[36]: set() In [39]: a = {1} In [40]: b = {1, 2} In [41]: a.intersection(b) Out[41]: {1} In [43]: a Out[43]: {1} In [44]: b = {1, 2, 3} In [45]: a.union(b) Out[45]: {1, 2, 3} In [45]: a.union(b) Out[45]: {1, 2, 3} In [46]: a & b Out[46]: {1} In [47]: a ^ b Out[47]: {2, 3} In [48]: a - b Out[48]: set() In [49]: b - a Out[49]: {2, 3}<BR><BR> ? 1 2 3 4 5 6 7 8 9 10 11 In [50]: a > b Out[50]: False In [51]: b > a Out[51]: True In [52]: a == b Out[52]: False In [53]: a != b Out[53]: True
以上這篇淺談python中set使用就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- python下setuptools的安裝詳解及No module named setuptools的解決方法
- Python 安裝setuptools和pip工具操作方法(必看)
- python在Windows下安裝setuptools(easy_install工具)步驟詳解
- 全面了解Python的getattr(),setattr(),delattr(),hasattr()
- Python中set與frozenset方法和區(qū)別詳解
- Python中內(nèi)置數(shù)據(jù)類型list,tuple,dict,set的區(qū)別和用法
- python中的set實(shí)現(xiàn)不重復(fù)的排序原理
相關(guān)文章
python opencv攝像頭的簡(jiǎn)單應(yīng)用
這篇文章主要為大家詳細(xì)介紹了python opencv攝像頭的簡(jiǎn)單應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06手動(dòng)安裝Anaconda環(huán)境變量的實(shí)現(xiàn)教程
這篇文章主要介紹了手動(dòng)安裝Anaconda環(huán)境變量的實(shí)現(xiàn)教程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Python如何設(shè)置指定窗口為前臺(tái)活動(dòng)窗口
這篇文章主要介紹了Python如何設(shè)置指定窗口為前臺(tái)活動(dòng)窗口,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08python處理xls文件openpyxl基礎(chǔ)操作
這篇文章主要為大家介紹了python處理xls文件openpyxl基礎(chǔ)操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Python Pingouin數(shù)據(jù)統(tǒng)計(jì)分析技術(shù)探索
Pingouin庫基于pandas、scipy和statsmodels,為用戶提供了執(zhí)行常見統(tǒng)計(jì)分析的功能,它支持各種統(tǒng)計(jì)方法和假設(shè)檢驗(yàn),例如 t-tests、ANOVA、correlation analysis 等,本文通過一些示例代碼,以更全面地了解如何使用Pingouin庫進(jìn)行統(tǒng)計(jì)分析,2024-01-01tensorflow 實(shí)現(xiàn)自定義layer并添加到計(jì)算圖中
今天小編就為大家分享一篇tensorflow 實(shí)現(xiàn)自定義layer并添加到計(jì)算圖中,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02