python數據類型-集合

總結:Python3.6版本

  • (無序結構,沒有索引遍歷)
  • 創建集合:set()、frozenset()【空集合,可變、不可變】
  • 創建集合:set(iterable)、frozenset(iterable)【新集合對象,可變、不可變】
  • 創建集合:{x for x in iterable}【集合解析創建集合】
  • 集合分類:單層、嵌套
  • 集合長度:len(set)
  • 成員測試:x in set【返回布爾值】、for x in set:print(x)【遍歷,不支持索引值遍歷】
  • 集合增加:set.add()【返回None,如果數據存在,則沒有任何影響】
  • 集合清空:set.clear()【返回None,清空集合】
  • 集合拷貝:set.copy()【返回一個淺拷貝的列表對象,單層拷貝與深拷貝的區別後邊解釋】
  • 集合刪除:set.discard()【返回None,如果給定的成員在集合中則刪除,否則無影響】
  • 集合刪除:set.remove()【返回None,如果給定的成員在集合中則刪除,否則拋出異常】
  • 集合刪除:set.pop()【返回刪除的成員,隨機刪除一個成員】
  • 集合比較:【運算符重載,>、=、<=、==、!=】--【 對應後邊運算的子集和超集,包含與被包含返回True】
  • 集合運算:【(-、-=、|、|=、&、&=、^、^=、)--差集,並集,交集,反交集,子集,超集】
  • 集合差集:set-set1、set.difference(set1)【返回set有的set1沒有的】
  • 集合差集:set-=set1set.difference_update(set1)【返回None,改變set成員為兩集合的差集】
  • 集合並集:set|set1、set.union(set1)【返回兩個集合所有成員並去重】
  • 集合並集:set|=set1set.update(set1)【返回None,改變set成員為兩個集合的並集】
  • 集合交集:set&set1、set.intersection(set1)【返回兩個集合都有的成員】
  • 集合交集:set&=set1、set.intersection_update(set1)【返回None,改變set成員為兩個集合的交集】
  • 集合交集:set.isdisjoint(set1)【兩集合交集為空返回True,否則返回False】
  • 集合反交集:set^set1、set.symmetric_difference(set1)【返回非兩個集合都有的成員】
  • 集合反交集:set^=set1、set.symmetric_difference_update(set1)【返回None,改變set成員為非兩個集合都有的成員】
  • 集合子集:set<set1>
  • 集合超集:set>set1 set.issuperset(set1)【set包含set1則返回True,否則返回False】
  • /<set1>


python數據類型-集合

<code># 創建集合【空集合,可變、不可變】
My_set = set()
My_set1 = frozenset()
print(My_set, My_set1) # set() frozenset()

# 創建集合【直接創建、集合解析創建、實例化類】
print({1, 2, 3}) # {1, 2, 3}
print({x for x in '123'}) # {'1', '2', '3'}
print(set('123')) # {'2', '3', '1'}
print(frozenset('123')) # frozenset({'2', '3', '1'})

# 集合分類【單層、嵌套】
# 嵌套不可以是列表或者集合【TypeError: unhashable type: 'list' and 'set' 】
print({1, '2', 3}) # {1, 3, '2'}
print({1, '2', (3, 4)}) # {1, (3, 4), '2'}

# 集合長度【返回長度,運算符重載方法,長度運算,相當於調用了__len__方法】
print(len({1, 2, 3})) # 3
print({1, 2, 3}.__len__()) # 3

# 成員測試【返回布爾值,運算符重載方法,成員關係測試in,相當於調用了__contains__方法】
print(3 in {1, 2, 3}) # True
for x in {1, 2, 3}:
print(x, end=" ") # 1 2 3
print({1, 2, 3}.__contains__(3)) # True

# 集合增加【返回None,如果數據存在,則沒有任何影響】
My_set = set('12')
print(My_set.add(1)) # None
print(My_set.add('1')) # None
print(My_set) # {1, '1', '2'}

# 集合清空【返回None,清空集合】

My_set = set('12')
print(My_set.clear()) # None
print(My_set) # set()

# 集合拷貝【返回一個淺拷貝的列表對象】
My_set = set('12')
My_set_copy = My_set.copy()
print(My_set_copy, My_set) # {'2', '1'} {'2', '1'}

# 集合刪除【返回None,如果給定的成員在集合中則刪除,否則無影響】
My_set = set('12')
print(My_set.discard(1)) # None
print(My_set.discard('1')) # None
print(My_set) # {'2'}

# 集合刪除【返回None,如果給定的成員在集合中則刪除,否則拋出異常】
My_set = set('12') # print(My_set.remove(1)) KeyError: 1
print(My_set.remove('1')) # None
print(My_set) # {'2'}

# 集合刪除【返回刪除的成員,隨機刪除一個成員】
My_set = set('abc')
print(My_set.pop()) # b
print(My_set) # {'c', 'a'}

# 集合比較【返回布爾值,運算符重載方法,特定的比較,調用方式(,<=,>=,==,!=)】
print({1, 2, 3} == {1, 3, 2}) # True
print({1, 2, 3}.__eq__({1, 2, 3})) # True

print({1, 2, 3} != {1, 3, 2}) # False
print({1, 2, 3}.__ne__({1, 2, 3})) # False

print({1, 2, 3} > {1, 3, 2}) # False
print({1, 2, 3}.__gt__({1, 2, 3})) # False

print({1, 2, 3} < {1, 3, 2}) # False
print({1, 2, 3}.__lt__({1, 2, 3})) # False

print({1, 2, 3} >= {1, 3, 2}) # True

print({1, 2, 3}.__ge__({1, 2, 3})) # True

print({1, 2, 3} <= {1, 3, 2}) # True
print({1, 2, 3}.__le__({1, 2, 3})) # True

# 集合運算【類方法和運算符重載方法】
# 集合差集
# 【運算符重載方法:__sub__,-調用、或調用intersection,不改變任何一個集合】
# 【運算符重載方法:__rsub__,-調用,區別value-self,不改變任何一個集合】
# 【運算符重載方法:__isub__,-=調用、或調用intersection_update,改變任何集合--調用者】
My_set = set('ab')
My_set1 = set('bc')
print(My_set - My_set1) # {'a'}
print(My_set, My_set1) # {'a', 'b'} {'b', 'c'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.__sub__(My_set1)) # {'a'}
print(My_set, My_set1) # {'b', 'a'} {'c', 'b'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.__rsub__(My_set1)) # {'c'} 等價於 My_set1 - My_set
print(My_set, My_set1) # {'b', 'a'} {'c', 'b'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.difference(My_set1)) # {'a'}
print(My_set, My_set1) # {'b', 'a'} {'c', 'b'}

My_set = set('ab')
My_set1 = set('bc')
My_set -= My_set1
print(My_set, My_set1) # {'a'} {'b', 'c'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.__isub__(My_set1)) # {'a'}
print(My_set, My_set1) # {'a'} {'b', 'c'}

My_set = set('ab')

My_set1 = set('bc')
print(My_set.difference_update(My_set1)) # None
print(My_set, My_set1) # {'a'} {'b', 'c'}

# 集合並集
# 【運算符重載方法:__or__,|調用、或調用union,不改變任何一個集合】
# 【運算符重載方法:__rsub__,|調用,區別value|self,不改變任何一個集合,集合無序,所以無影響】
# 【運算符重載方法:__ior__,|=調用、或調用update,改變任何集合--調用者】
My_set = set('ab')
My_set1 = set('bc')
print(My_set | My_set1) # {'b', 'a', 'c'}
print(My_set, My_set1) # {'b', 'a'} {'b', 'c'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.__or__(My_set1)) # {'b', 'a', 'c'}
print(My_set, My_set1) # {'b', 'a'} {'b', 'c'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.__ror__(My_set1)) # {'b', 'a', 'c'}
print(My_set, My_set1) # {'b', 'a'} {'b', 'c'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.union(My_set1)) # {'c', 'b', 'a'}
print(My_set, My_set1) # {'b', 'a'} {'c', 'b'}

My_set = set('ab')
My_set1 = set('bc')
My_set |= My_set1
print(My_set, My_set1) # {'b', 'a', 'c'} {'b', 'c'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.__ior__(My_set1)) # {'b', 'a', 'c'}
print(My_set, My_set1) # {'b', 'a', 'c'} {'b', 'c'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.update(My_set1)) # None
print(My_set, My_set1) # {'b', 'c', 'a'} {'b', 'c'}


# 集合交集
# 【isdisjoint:兩集合交集為空返回True,否則返回False】
# 【運算符重載方法:__and__,&調用、或調用intersection,不改變任何一個集合】
# 【運算符重載方法:__rand__,&調用,區別是value&self,不改變任何一個集合,集合無序,所以無影響】
# 【運算符重載方法:__iand__,&=調用、或調用intersection_update,改變任何集合--調用者】
My_set = set('ab')
My_set1 = set('bc')
print(My_set.isdisjoint(My_set1)) # False

My_set = set('ab')
My_set1 = set('bc')
print(My_set & My_set1) # {'b'}
print(My_set, My_set1) # {'a', 'b'} {'c', 'b'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.__and__(My_set1)) # {'b'}
print(My_set, My_set1) # {'a', 'b'} {'c', 'b'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.__rand__(My_set1)) # {'b'}
print(My_set, My_set1) # {'a', 'b'} {'c', 'b'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.intersection(My_set1)) # {'b'}
print(My_set, My_set1) # {'a', 'b'} {'c', 'b'}

My_set = set('ab')
My_set1 = set('bc')
My_set &= My_set1
print(My_set) # {'b'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.__iand__(My_set1)) # {'b'}
print(My_set) # {'b'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.intersection_update(My_set1)) # None
print(My_set, My_set1) # {'b'} {'c', 'b'}

# 集合反交集
# 【運算符重載方法:__xor__,調用^,或者symmetric_difference函數調用,不改變任何一個集合】
# 【運算符重載方法:__rxor__,調用^,區別是value&self,但集合是無序的,所以無影響,或者我們顛倒集合計算】
# 【運算符重載方法:__ior__,運算符^=,或者symmetric_difference_update函數調用,改變任何集合--調用者】
My_set = set('ab')
My_set1 = set('bc')
print(My_set ^ My_set1) # {'c', 'a'}
print(My_set, My_set1) # {'a', 'b'} {'b', 'c'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.__xor__(My_set1)) # {'c', 'a'} 【或者 My_set.__rxor__(My_set1)】
print(My_set, My_set1) # {'a', 'b'} {'b', 'c'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.symmetric_difference(My_set1)) # {'a', 'c'}
print(My_set, My_set1) # {'a', 'b'} {'c', 'b'}

My_set = set('ab')
My_set1 = set('bc')
My_set ^= My_set1
print(My_set, My_set1) # {'a', 'c'} {'c', 'b'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.__ixor__(My_set1)) # {'a', 'c'}
print(My_set, My_set1) # {'a', 'c'} {'c', 'b'}

My_set = set('ab')
My_set1 = set('bc')
print(My_set.symmetric_difference_update(My_set1)) # None
print(My_set, My_set1) # {'a', 'c'} {'c', 'b'}

# 集合子集

My_set = set('a')
My_set1 = set('ab')
print(My_set < My_set1) # True
print(My_set.issubset(My_set1)) # True

# 集合超集
My_set = set('ab')
My_set1 = set('a')
print(My_set > My_set1) # True
print(My_set.issuperset(My_set1)) # True/<code>

原型類或者方法:

<code>class set(object):
set() -> new empty set object
set(iterable) -> new set object
#空參數返回空集合、參數為可迭代對象創建一個集合對象
Build an unordered collection of unique elements.

def add(self, *args, **kwargs):
Add an element to a set.
This has no effect if the element is already present.
pass
#返回None,數據不存在,則增加數據到集合,如果數據存在,則沒有任何影響

def clear(self, *args, **kwargs):
Remove all elements from this set.
pass
#清空集合的數據

def copy(self, *args, **kwargs):
Return a shallow copy of a set.
pass
#淺拷貝一個集合

def difference(self, *args, **kwargs):
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.)
pass
#返回兩個集合的差集 a-b,如果多個集合比較則只能用這個函數a.difference(b,c)


def difference_update(self, *args, **kwargs):
Remove all elements of another set from this set.
pass
#將存在於調用者這個集合中的也屬於另一個集合的數據去除掉,修改了調用者這個集合

def discard(self, *args, **kwargs):
Remove an element from a set if it is a member.
If the element is not a member, do nothing.
pass
#返回None,如果給定的成員在集合中則刪除,否則無影響

def intersection(self, *args, **kwargs):
Return the intersection of two sets as a new set.
(i.e. all elements that are in both sets.)
pass
#返回兩個集合的交集--一個新集合,a & b ,不改變原有兩個集合

def intersection_update(self, *args, **kwargs):
Update a set with the intersection of itself and another.
pass
返回None,改變調用者這個集合的成員為兩個集合的交集

def isdisjoint(self, *args, **kwargs):
Return True if two sets have a null intersection.
pass
#返回True如果兩個集合沒有交集

def issubset(self, *args, **kwargs):
Report whether another set contains this set.
pass
#子集判斷 a
def issuperset(self, *args, **kwargs):
Report whether this set contains another set.
pass
#超集判斷 a>b a.issuperset(b) 滿足條件返回True


def pop(self, *args, **kwargs):
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
pass
#隨機刪除一個成員,集合為空則拋出keyerror

def remove(self, *args, **kwargs):
Remove an element from a set; it must be a member.
If the element is not a member, raise a KeyError.
pass
#返回None,如果給定的成員在集合中則刪除,否則拋出異常

def symmetric_difference(self, *args, **kwargs):
Return the symmetric difference of two sets as a new set.
(i.e. all elements that are in exactly one of the sets.)
pass
#反交集運算 a ^ b

def symmetric_difference_update(self, *args, **kwargs):
Update a set with the symmetric difference of itself and another.
pass
#將調用者這個集合的值更改為反交集運算的結果

def union(self, *args, **kwargs):
Return the union of sets as a new set.
(i.e. all elements that are in either set.)
pass
#並集運算 a | b

def update(self, *args, **kwargs):
Update a set with the union of itself and others.
pass
#將調用者這個集合的值更改為並集運算的結果

def __and__(self, *args, **kwargs):
Return self&value.
pass
#運算符重載方法 &調用

def __contains__(self, y): ;

x.__contains__(y) <==> y in x.
pass
#運算符重載方法 in調用

def __eq__(self, *args, **kwargs):
Return self==value.
pass
#運算符重載方法 ==調用

#def __getattribute__(self, *args, **kwargs):
Return getattr(self, name).
pass

def __ge__(self, *args, **kwargs):
Return self>=value.
pass
#運算符重載方法 >=調用

def __gt__(self, *args, **kwargs):
Return self>value.
pass
#運算符重載方法 >調用

def __iand__(self, *args, **kwargs):
Return self&=value.
pass
#運算符重載方法 &=調用,等於調用intersection_update
a &= b 不等價於 a = a &b

def __init__(self, seq=()): # known special case of set.__init__
set() -> new empty set object
set(iterable) -> new set object
Build an unordered collection of unique elements.
# (copied from class doc)
pass
#創建對象是調用 -運算符重載方法 構造函數

def __ior__(self, *args, **kwargs):
Return self|=value.
pass
#運算符重載方法 |=調用

def __isub__(self, *args, **kwargs):
Return self-=value.
pass
#運算符重載方法 -=調用

def __iter__(self, *args, **kwargs):
Implement iter(self).
pass

def __ixor__(self, *args, **kwargs):
Return self^=value.
pass
#運算符重載方法 ^=調用
def __len__(self, *args, **kwargs):
Return len(self).
pass
#長度計算 len調用

def __le__(self, *args, **kwargs):
Return self<=value.
pass
#運算符重載方法 <=調用

def __lt__(self, *args, **kwargs):
Return self<value.> pass
#運算符重載方法
@staticmethod # known case of __new__
def __new__(*args, **kwargs):
Create and return a new object. See help(type) for accurate signature.
pass
#創建時調用??。

def __ne__(self, *args, **kwargs):
Return self!=value.
pass
#運算符重載方法 !=調用

def __or__(self, *args, **kwargs):
Return self|value.
pass
#運算符重載方法 |調用

def __rand__(self, *args, **kwargs):
Return value&self.

pass
#運算符重載方法 &調用

def __reduce__(self, *args, **kwargs):
Return state information for pickling.
pass

def __repr__(self, *args, **kwargs):
Return repr(self).
pass

def __ror__(self, *args, **kwargs):
Return value|self.
pass
#運算符重載方法 |調用

def __rsub__(self, *args, **kwargs):
Return value-self.
pass
#運算符重載方法 -調用

def __rxor__(self, *args, **kwargs):
Return value^self.
pass
#運算符重載方法 ^調用

def __sizeof__(self): ; restored from __doc__
S.__sizeof__() -> size of S in memory, in bytes
pass

def __sub__(self, *args, **kwargs):
Return self-value.
pass
#運算符重載方法 -調用

def __xor__(self, *args, **kwargs):
Return self^value.
pass
#運算符重載方法 ^調用
__hash__ = None/<value.>
/<code>


分享到:


相關文章: