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

歡迎訪問 生活随笔!

生活随笔

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

python

python3 set_python3.x 基础三:set集合

發(fā)布時間:2023/12/9 python 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3 set_python3.x 基础三:set集合 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

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

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

set()

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

|????? 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'}

總結(jié)

以上是生活随笔為你收集整理的python3 set_python3.x 基础三:set集合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。