Python學習-集合的常見用法

st = [1,2,3,4,5]
ct = [2,3,4,5,76]
list = set(["name", 'list', 'try'])
list2 = set(["name", 'list', 'try', 'but', 'test'])
# 兩個列表去重,利用集合

st = set(st)   #設為集合
ct = set(ct)
print(st, type(st))sct0 = st.union(ct)  #并集
sct = st | ct   #并集

sct2 = st.intersection(ct) #交集
sct1 = st & ct  #交集

sct3 = st.difference(ct) #差集
sct4 = st - ct   #差集,st中減去ct中的元素print(sct0)
print(sct)
print(sct1)
print(sct2)print(sct3)
print(sct4)print(list.intersection(list2))#子集
sct_0 = set([2, 3])
print(sct_0.issubset(st))  #判斷前者是否為后者的子集
print(sct_0.issuperset(st))#判斷前者是否為后者的父集#對稱差集
print(st.symmetric_difference(ct))  # 去掉兩者的并集
print(st.copy())cp_st = st.copy()
print(cp_st)#添加單個元素
cp_st.add(11)
print(cp_st)
print('%s st list is here' % st) #會發現st中沒有變
#添加多個元素,位置是隨機的
cp_st.update([11, 22, 'key'])
print(cp_st)#判斷是否交集是空
print(cp_st.isdisjoint(list2))#去除一個元素
cp_st.remove(11)
print(cp_st)cp_st.add("str")
print(cp_st)
cp_st.remove('str')
print(cp_st)cp_st.pop()   #這個算是隨機刪除
print(cp_st)cp_st.discard('key')  #指定刪除哪一個,最好是數字,但是不是數字也能用。會有提示
print(cp_st)

?

轉載于:https://www.cnblogs.com/Ian-learning/p/7827852.html

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

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

相關文章

Autofac之自動裝配

從容器中的可用服務中選擇一個構造函數來創造對象,這個過程叫做自動裝配。這個過程是通過反射實現的 默認 思考這么一個問題,如果注冊類型中存在多個構造函數,那么Autofac會選擇哪一個來創建類型的實例 答案是"盡可能最多參數" class ConstructorClass {p…

對Emlog 6.0 Beta的完整代碼審計過程

Emlog 6.0 beta版本,這可能是最后一篇關于PHP語言CMS的代碼審計文章,此次將詳細記錄完整的審計過程。 文章基本上完整記錄小東的對此CMS審計過程,或許顯得繁瑣,但代碼審計的過程就是這樣,發現可能項,然后精…

SINOCES 2011

突然發現又好久沒寫過日志了 是在是太懶了… 難得休假去看了眼消費電子 感覺實在是一年不如一年 佳能、索尼不見蹤影,相機滿場沒見一家(大牌子是真沒見到) 華碩技嘉微星等主板廠商同樣失蹤… PC方面,聯想貌似是來賣電腦包鼠標的&a…

esim卡與ms卡的區別_什么是eSIM,它與SIM卡有何不同?

esim卡與ms卡的區別With the launch of the Apple Watch 3, the term “eSIM” has been thrown around a lot. And now, Google’s Pixel 2 is the first phone to use this new technology, it’s time we take a closer look at what it is, what it does, and what this me…

機器學習實戰之logistic回歸分類

利用logistic回歸進行分類的主要思想:根據現有數據對分類邊界建立回歸公式,并以此進行分類。 logistic優缺點: 優點:計算代價不高,易于理解和實現。缺點:容易欠擬合,分類精度可能不高。 .適用數…

HDU 6343.Problem L. Graph Theory Homework-數學 (2018 Multi-University Training Contest 4 1012)

6343.Problem L. Graph Theory Homework 官方題解: 一篇寫的很好的博客: HDU 6343 - Problem L. Graph Theory Homework - [(偽裝成圖論題的)簡單數學題] 代碼: 1 //1012-6343-數學2 #include<iostream>3 #include<cstdio>4 #include<cstring>5 #include<…

Android GridView LruCache

照片墻這種功能現在應該算是挺常見了&#xff0c;在很多應用中你都可以經常看到照片墻的身影。它的設計思路其實也非常簡單&#xff0c;用一個GridView控件當作“墻”&#xff0c;然后隨著GridView的滾動將一張張照片貼在“墻”上&#xff0c;這些照片可以是手機本地中存儲的&a…

如何在Android TV上自定義推薦行

When you fire up Android TV, the first thing you see is a list of movies and shows the system thinks you’ll like. It’s often full of the latest flicks or hottest news, but sometimes it could just be things relevant to your interests and the apps you have…

遞歸 段錯誤 習題

段錯誤 遞歸里面算階乘 f(10000000)沒有輸出&#xff0c;使用gdb 顯示 SIGSEGV--段錯誤編譯后產生的可執行文件里面保存著什么&#xff1f;UNIX/Linux 用 ELFDOS下用COFFWindows用PE&#xff08;COFF擴充而得&#xff09;段&#xff08;segmentation&#xff09;二進制文件內的…

你知道你常用的dos和linux命令嗎?

功能 Linux MS-DOS 進入到該目錄 cd cd 列舉文件 ls dir 創建目錄 mkdir mkdir 清除屏幕 clear cls 復制文件 cp copy 移動文件 mv move 刪除文件 rm del 查看文件 less more 文件重命名 mv ren 比較文件內容 diff fc 查看當前路徑 pwd chd…

steam串流到手機_如何從手機將Steam游戲下載到PC

steam串流到手機Steam allows you to remotely install games from your smartphone, just like you can with a PlayStation 4 or Xbox One. You can download games to your gaming PC from anywhere, ensuring those big downloads are complete and the game is ready to p…

編寫安裝配置ftp-samba服務腳本

本腳本實例的要求如下&#xff1a; 1、公司有公共共享目錄public,所有員工均可讀寫&#xff0c;但不允許刪除其他員工的文件;不能匿名登錄 2、每部門均有共享目錄&#xff0c;部門經理可讀寫&#xff0c;部門員工可讀&#xff1b; 非本部門員工不能訪問&#xff08;caiwu、rens…

利用java實現excel轉pdf文件

在有些需求當中我們需要抓取字段并且填充到excel表格里面&#xff0c;最后將excel表格轉換成pdf格式進行輸出&#xff0c;我第一次接觸這個需求時&#xff0c;碰到幾個比較棘手的問題&#xff0c;現在一一列出并且提供解決方案。 1&#xff1a;excel轉pdf出現亂碼&#xff1a; …

Jmeter HTTP請求后響應數據顯示亂碼解決方法

Jmeter請求后結果樹里無論是text還是html響應數據顯示亂碼&#xff0c;這是因為jmeter 編碼格式配置文件默認不開啟導致的&#xff0c;解決方法如下&#xff1a; 1&#xff09;進入jmeter-***\bin目錄下&#xff0c;找到jmeter.properties文件&#xff0c;以文本文件形式打開 2…

禁用windows10更新_如何在Windows 10中禁用投影

禁用windows10更新The drop shadows on applications in the Windows 10 preview are really big and suspiciously similar to the ones in OS X, and if they aren’t your speed, you can easily remove them. We actually think they look good, but since somebody out th…

如何訪問 Service?- 每天5分鐘玩轉 Docker 容器技術(99)

前面我們已經學習了如何部署 service&#xff0c;也驗證了 swarm 的 failover 特性。不過截止到現在&#xff0c;有一個重要問題還沒有涉及&#xff1a;如何訪問 service&#xff1f;這就是本節要討論的問題。 為了便于分析&#xff0c;我們重新部署 web_server。 ① docker se…

sqlyog下載

sqlyog下載&#xff08;附注冊碼&#xff09;&#xff1a;http://www.onlinedown.net/soft/24926.htm轉載于:https://www.cnblogs.com/shujuxiong/p/9474496.html

Linux配置手冊(二)配置DHCP服務器

1.檢查是否安裝DHCP服務器軟件 2.掛在RHEL5系統光盤 3.安裝DHCP服務軟件 4.將模板配置文件復制并覆蓋現在的配置文件 5.配置修改dhcpd.conf文件 配置信息 默認租約時間 default-lease-time 最大租約時間 max-lease-time 局域網內所有主機的域名 option domain-name 客戶機所使用…

什么是Google Play保護以及如何確保Android安全?

Android is open, flexible, and all about choice. Unfortunately, that flexibility comes more potential security issues. The good news is that Google has a system in place named Play Protect that helps keep Android secure. Android開放&#xff0c;靈活且具有多…

如何使計算機為您讀取文檔

Since the beginning of the computer age, people have always enjoyed making computers talk to them. These days, that functionality is built right into Windows and you can easily use it to have your PC read documents to you. 自計算機時代開始以來&#xff0c;人…