python3 set_python3.x 基礎三:set集合

|? clear(...) 清空一個集合

|????? Remove all elements from this set.

>>>set1.clear()>>>set1

set()

|? copy(...) 影子復制,指向同一個內存地址

|????? Return a shallow copy of a set. |

>>> list1

[3, 2, 1, 1, 2, 3, 4, 5]

>>> list2

[3, 4, 5, 6, 7, 8]

>>> set1=set(list1)>>>id(set1)140138768485512>>> set3=set1.copy()>>>id(set3)140138695576712

|? difference(...) 差集,格式set1.difference(set2),求in list1 not in list2的集合

|????? Return the difference of two or more sets as a new set. |

|????? (i.e. all elements that are in this set but not the others.)

>>>set1

{1, 2, 3, 4, 5}>>>set2

{3, 4, 5, 6, 7, 8}>>>set1.difference(set2)

{1, 2}

|? difference_update(...) 刪除在本集合同時也在其他集合的元素,差集

|????? Remove all elements of another set from this set. |

>>> set1=set(list1)

>>> set2=set(list2)

>>> set1,set2

({1, 2, 3, 4, 5}, {3, 4, 5, 6, 7, 8})

>>> set1=set(list1)>>> set2=set(list2)>>>set2.difference_update(set1)>>>set2

{6, 7, 8}

|? discard(...) 刪除一個在本集合中的元素

|????? Remove an element from a set if it is a member.

|

|????? If the element is not a member, do nothing. |

>>> set3=set([1,2,3,'a','b','c'])>>> set3.discard(1)>>>set3

{2, 3, 'c', 'b', 'a'}>>> set3.discard('a')>>>set3

{2, 3, 'c', 'b'}>>> set3.discard('dd')>>>set3

{2, 3, 'c', 'b'}

|? intersection(...)并集,同時在兩個集合中的元素

|????? Return the intersection of two sets as a new set.

|

|????? (i.e. all elements that are in both sets.) |

>>>set1

{1, 2, 3, 4, 5}>>>set2

{3, 4, 5, 6, 7, 8}>>>set1.intersection(set2)

{3, 4, 5}>>>set2.intersection(set1)

{3, 4, 5}

|? intersection_update(...) 交集

|????? Update a set with the intersection of itself and another. |

>>>set1,set2

({1, 2, 3, 4, 5}, {3, 4, 5, 6, 7, 8})>>>set1.intersection_update(set2)>>>set1

{3, 4, 5}

|? isdisjoint(...) 返回布爾值,判斷兩個集合是否沒有并集

|????? Return True if two sets have a null intersection.

|

>>>set1,set2,set3,set4

({3, 4, 5}, {3, 4, 5, 6, 7, 8}, {2, 3, 'c', 'b'}, {'y', 'x', 'z'})>>>set1.isdisjoint(set2)

False>>>set1.isdisjoint(set4)

True

|? issubset(...) 返回布爾值,判斷前一個集合是否是后一個集合的子集

|????? Report whether another set contains this set. |

>>>set1

{3, 4, 5}>>>set5

{3, 4}>>>set5.issubset(set1)

True

|? issuperset(...) 返回布爾值,判斷前一個集合是否是后一個集合的父集

|????? Report whether this set contains another set. |

>>>set1

{3, 4, 5}>>>set5

{3, 4}>>>set1.issuperset(set5)

True

|? pop(...) 隨機刪除一個集合元素,返回被刪除元素,空集合刪除則報錯

|????? Remove and return an arbitrary set element.

|????? Raises KeyError if the set is empty. |

>>>set1

{3, 4, 5}>>>set1.pop()3

>>>set1

{4, 5}>>>set1.pop()4

>>>set1.pop()5

>>>set1.pop()

Traceback (most recent call last):

File"", line 1, in KeyError:'pop from an empty set'

|? remove(...) 刪除指定在集合中的元素

|????? Remove an element from a set; it must be a member. |

>>> set1=set(list1)>>>set1

{1, 2, 3, 4, 5}>>> set1.remove(1)>>>set1

{2, 3, 4, 5}>>> set1.remove('a')

Traceback (most recent call last):

File"", line 1, in KeyError:'a'

|? symmetric_difference(...) 對稱差集,集合A與集合B不相交的部分,交集的反集

| ? ??Return the symmetric difference of two sets as a new set.

|???? (i.e. all elements that are in exactly one of the sets.)

>>>set1

{1, 2, 3, 4, 5}>>> set6={4,5,6,7,8}>>>set1.symmetric_difference(set6)

{1, 2, 3, 6, 7, 8}>>>set6.symmetric_difference(set1)

{1, 2, 3, 6, 7, 8}

>>> set1,set7

({1, 2, 3, 4, 5}, {'a', 'c', 'b'})

>>> set1.symmetric_difference(set7)

{'c', 2, 3, 1, 4, 5, 'b', 'a'}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/396338.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/396338.shtml
英文地址,請注明出處:http://en.pswp.cn/news/396338.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux內核分析作業第八周

進程的切換和系統的一般執行過程 一、進程調度的時機 中斷處理過程(包括時鐘中斷、I/O中斷、系統調用和異常)中,直接調用schedule(),或者返回用戶態時根據need_resched標記調用schedule(); 內核線程可以直接調用sched…

iOS--數據存儲NSUserDefaults

2019獨角獸企業重金招聘Python工程師標準>>> 今天去面試,被問道NSUserDefaults的存取并手寫出來,一時想不起來,回來之后看看之前的筆記,稍作一些整理 NSUserDefaults是一個單例,在整個程序中只有一個實例對…

巨人肩膀_如何站在巨人的肩膀上

巨人肩膀“If I have seen further than others, it is by standing on the shoulders of giants.” — Isaac Newton“如果我能比其他人看到更多,那就是站在巨人的肩膀上。” —艾薩克牛頓 In 1676, Isaac Newton spoke of the great thinkers who came before him…

mysql 觸發器定義變量_MySQL 函數存儲過程觸發器定義簡單示例

1.變量提示NEW 是新值-- OLD 是舊值INSERT 只有NEW ----UPDATE有NEW和OLD ---DELETE只有OLD2.準備測試表(userinfo、userinfolog)use test;create table userinfo(userid int,username varchar(10),userbirthday date);create table userinfolog(logtime datetime,loginfo varc…

[EOJ439] 強制在線

Description 見EOJ439 Solution 先考慮不強制在線怎么做。 按詢問區間右端點排序&#xff0c;從左往右掃&#xff0c;維護所有后綴的答案。 如果掃到 \(a[i]\)&#xff0c;那么讓統計個數的 \(cnt[a[i]]\). 如果\(cnt[a[i]]<a[i]\)&#xff0c;那么在當前的右端點固定的情況…

大數據 就業 缺口_中國AI&大數據就業趨勢報告:平均月薪超2萬,缺口650萬人...

2019世界人工智能大會開幕式上&#xff0c;特斯拉公司聯合創始人兼首席執行官Elon Musk 和中國企業家俱樂部主席、聯合國數字合作高級別小組聯合主席馬云進行了一場“雙馬”對話。談到人工智能話題時&#xff0c;馬斯克認為&#xff0c;“未來的科技發展變化將超越我們的能力”…

Android pm 命令詳解

一、pm命令介紹與包名信息查詢 1.pm命令介紹 pm工具為包管理&#xff08;package manager&#xff09;的簡稱 可以使用pm工具來執行應用的安裝和查詢應用寶的信息、系統權限、控制應用 pm工具是Android開發與測試過程中必不可少的工具&#xff0c;shell命令格式如下&#xff1a…

開源 非開源_開源為善

開源 非開源by Michael D. Johnson邁克爾約翰遜(Michael D.Johnson) 開源為善 (Open Source for Good) We’ve spent two years coding for a cause, one nonprofit at a time. And now Free Code Camp’s pushing ahead to help organizations at scale.我們花了兩年的時間為…

mysql5.6熱升級_Mysql5.6主從熱備配置

數據庫是應用系統的核心&#xff0c;為了保證數據庫的安全采用主從熱備是很常見的方法&#xff0c;也就是主數據庫DDL、DML都將被同步到從數據庫。一、 實驗環境操作系統&#xff1a;windowsserver 2008 R2數據庫&#xff1a;mysql-advanced-5.6.21-winx64二、 準備工作1、…

InfluxDB(官方使用說明)

安裝InfluxDB OSS 此頁面提供有關安裝&#xff0c;啟動和配置InfluxDB的說明。 InfluxDB OSS安裝要求 root為了成功完成&#xff0c;需要安裝InfluxDB軟件包或具有管理員權限。 InfluxDB OSS網絡端口 InfluxDB默認使用以下網絡端口&#xff1a; TCP端口8086用于通過InfluxDB的H…

incc與oracle連接_Oracle 連接和會話的區別

連接并不是會話的同義詞&#xff0c;發現這一點時很多人都很詫異。在大多數人眼里&#xff0c;它們都是一樣的&#xff0c;但事實上并不一定如此。在一條連接上可以建立0個、一個或多個會話。各個會話是單獨而且獨立的&#xff0c;即使它們共享同一條數據庫物理連接也是如此。一…

CodeForces 176B Word Cut(DP)

題意&#xff1a;給你a串和b串&#xff0c;你能切k次&#xff0c;每次切完將尾部分放在頭的前面&#xff0c;問有多少種方案切k次從a串變為b串 思路&#xff1a;令dp[i][0]為砍了i次變成b串的方案數&#xff0c;dp[i][1]為砍了i次變成非b串的方案數&#xff0c;然后預處理一下前…

如何將React App轉換為React Native

I have been working on a lot of mobile projects lately?—?including Cordova, PhoneGap, React Native, some Ionic and Swift?—?but I have to say, React Native is by far the best experience in mobile development I have had so far. It has great, web-like d…

HTTP狀態碼:400\500 錯誤代碼

轉自&#xff1a;http://blog.sina.com.cn/s/blog_59b052fa0100it74.html一些常見的狀態碼為&#xff1a;200 - 服務器成功返回網頁404 - 請求的網頁不存在503 - 服務不可用詳細分解&#xff1a;1xx&#xff08;臨時響應&#xff09;表示臨時響應并需要請求者繼續執行操作的狀態…

dhcp服務

安裝與配置 配置文件 修改配置文件 復制這個文件到另一端 打開另一端的配置文件 原端輸入這些命令可以去掉英文 然后vim進入另一端配置文件 全局配置不在{}內的 分發范圍是指哪個ip到哪個ip的范圍 指定固定電腦獲取固定位置 原端修改配置文件 下面進行啟動dhcp 克隆一臺虛擬機&…

python數據結構與算法40題_Python數據結構與算法40:遞歸編程練習題3:ASCII謝爾賓斯基地毯...

注&#xff1a;本文如涉及到代碼&#xff0c;均經過Python 3.7實際運行檢驗&#xff0c;保證其嚴謹性。本文閱讀時間約為7分鐘。遞歸編程練習題3&#xff1a;ASCII謝爾賓斯基地毯謝爾賓斯基地毯謝爾賓斯基地毯是形如上圖的正方形分形圖案&#xff0c;每個地毯可分為等大小的9份…

使用Python發送電子郵件

by Arjun Krishna Babu通過Arjun Krishna Babu 如何使用Python發送電子郵件 (How to send emails using Python) As a learning exercise, I recently dug into Python 3 to see how I could fire off a bunch of emails. There may be more straightforward methods of doing…

此blog不更了

1轉載于:https://www.cnblogs.com/ybai62868/p/5384097.html

Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart)

在接觸WebService時值得收藏的一篇文章&#xff1a; 在調試Axis1.4訪問WebService服務時&#xff0c;出現以下錯誤&#xff1a; Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart) 有錯誤找到錯誤原因以及發現值得收藏的…

java遍歷樹結構數據_Java數據結構——二叉樹的遍歷(匯總)

二叉樹的遍歷分為深度優先遍歷(DFS)和廣度優先遍歷(BFS)DFS遍歷主要有&#xff1a;前序遍歷中序遍歷后序遍歷一、遞歸實現DFSNode.java:public class Node {private Object data;Node richild;Node lechild;public Object getData() {return data;}public void setData(Object …