日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python集合运算_python-集合及其运算

發布時間:2025/3/21 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python集合运算_python-集合及其运算 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
列表:list_1 = [1,4,5,7,3,6,7,9]list_2 =set([2,6,0,66,22,8,4])list_3 = set([1,3,7])list_4 = set([5,6,7,8])

1、通過set變為集合并去重,集合也是無序的。

list_1 = set(list_1)

通過type查看list_1的類型

print(list_1,type(list_1))

運行結果:

{1, 3, 4, 5, 6, 7, 9}

2、取list1和list2的交集

print( list_1.intersection(list_2) )或者print(list_1 & list_2)

運行結果:

{4, 6}

3、取list1和list2的并集

print(list_1.union(list_2))或者print(list_2 | list_1)

運行結果:

{0, 1, 2, 66, 4, 3, 6, 5, 8, 7, 9, 22}

4、取list1和list2的差集

print(list_1.difference(list_2))、print(list_2.difference(list_1))或者

print(list_1 - list_2)

運行結果:

{1, 3, 5, 7, 9}、{0, 2, 66, 8, 22}

{1, 3, 5, 7, 9}

5、取list1h和list3的子集

#list3是list1的子集:print(list_3.issubset(list_1))

#list1是list3的父集:print(list_1.issuperset(list_3))

運行結果:

True

True

6、對稱差集,將兩個列表都有的部分去掉

print(list_1.symmetric_difference(list_2))或者print(list_1 ^ list_2)

運行結果:

{0, 1, 2, 66, 3, 5, 7, 8, 9, 22}


集合的增刪改查

1、增加

list_1.add(999)#增加一項

list_1.update([888,777,555])#增加多項

print(list_1)

運行結果:

{1, 3, 4, 5, 6, 7, 999, 9, 777, 555, 888}

2、刪除

print(list_1.pop())

運行結果:

1

3、刪除,不返回結果

list_1.discard(888)

運行結果:

None

總結

以上是生活随笔為你收集整理的python集合运算_python-集合及其运算的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。