標籤:建立 add 並集 intersect 批量 class 函數 count 索引
一、集合的定義
set集合,是一個無序且不重複的元素集合。
集合對象是一組無序排列的可雜湊的值,集合成員可以做字典中的鍵。集合支援用in和not in操作符檢查成員,由len()內建函數得到集合的基數(大小), 用 for 迴圈迭代集合的成員。但是因為集合本身是無序的,不可以為集合建立索引或執行切片(slice)操作,也沒有鍵(keys)可用來擷取集合中元素的值。
二、集合的建立
s = set()s = {11,22,33,44}*註:建立空集合時,只能用set(),如果用第二種方法s={},建立的實際上是一個空字典。s = {}print(type(s))<class ‘dict‘>a=set(‘boy‘)b=set([‘y‘, ‘b‘, ‘o‘,‘o‘])c=set({"k1":‘v1‘,‘k2‘:‘v2‘})d={‘k1‘,‘k2‘,‘k2‘}e={(‘k1‘, ‘k2‘,‘k2‘)}print(a,type(a))print(b,type(b))print(c,type(c))print(d,type(d))print(e,type(e))執行結果如下:{‘o‘, ‘b‘, ‘y‘} <class ‘set‘>{‘o‘, ‘b‘, ‘y‘} <class ‘set‘>{‘k1‘, ‘k2‘} <class ‘set‘>{‘k1‘, ‘k2‘} <class ‘set‘>{(‘k1‘, ‘k2‘, ‘k2‘)} <class ‘set‘>
三、集合的功能 源碼
準系統:
a=set(‘python‘)a.add(‘tina‘)print(a)b=set(‘python‘)b.update(‘tina‘)print(b)執行結果如下:{‘tina‘, ‘o‘, ‘p‘, ‘n‘, ‘t‘, ‘y‘, ‘h‘}{‘o‘, ‘i‘, ‘p‘, ‘a‘, ‘n‘, ‘t‘, ‘y‘, ‘h‘}##################由以上代碼可以看出,add是單個元素的添加,而update是批量的添加。輸出結果是無序的,並非添加到尾部。
c={‘p‘, ‘i‘, ‘h‘, ‘n‘, ‘o‘, ‘y‘, ‘t‘}c.remove(‘p‘)print(c)c={‘p‘, ‘i‘, ‘h‘, ‘n‘, ‘o‘, ‘y‘, ‘t‘}c.discard(‘p‘)print(c)c={‘p‘, ‘i‘, ‘h‘, ‘n‘, ‘o‘, ‘y‘, ‘t‘}c.pop()print(c)執行結果如下: {‘i‘, ‘h‘, ‘t‘, ‘o‘, ‘y‘, ‘n‘} #####當執行c.remove(‘p‘,‘i‘)和c.discard(‘p‘,‘i‘)時,報錯:TypeError: remove() takes exactly one argument (2 given),說明remove和discard刪除元素時都只能一個一個的刪,同add對應。#################################################################################remove,pop和discard的區別:discard刪除指定元素,當指定元素不存在時,不報錯;remove刪除指定元素,但當指定元素不存在時,報錯:KeyError。pop刪除任意元素,並可將移除的元素賦值給一個變數,不能指定元素移除。
set的特有功能:
s1 = {0}s2 = {i % 2 for i in range(10)}s = set(‘hi‘)t = set([‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘])print(s.intersection(t), s & t) # 交集print(s.union(t), s | t) # 並集print(s.difference(t), s - t) # 差集print(s.symmetric_difference(t), s ^ t) # 對稱差集print(s1.issubset(s2), s1 <= s2) # 子集(被包含)print(s1.issuperset(s2), s1 >= s2) # 父集(包含)執行結果如下:{‘h‘} {‘h‘}{‘i‘, ‘e‘, ‘h‘, ‘l‘, ‘o‘} {‘i‘, ‘e‘, ‘h‘, ‘l‘, ‘o‘}{‘i‘} {‘i‘}{‘e‘, ‘l‘, ‘o‘, ‘i‘} {‘e‘, ‘l‘, ‘o‘, ‘i‘}True TrueFalse Falses = {11,22,33}t = {22,44}print(s.isdisjoint(t))#(disjoint脫節的,)即如果沒有交集,返回True,否則返回Falses.difference_update(t)#將差集覆蓋到源集合,即從當前集合中刪除和B中相同的元素print(s)執行結果如下:False{33, 11}s = {11,22,33}t = {22,44}s.intersection_update(t)#將交集覆蓋到源集合print(s)執行結果如下:{22}s = {11,22,33}t = {22,44}s.symmetric_difference_update(t)#將對稱差集覆蓋到源集合print(s)執行結果如下:{33, 11, 44}四、練習題
尋找差異:哪些需要刪除?哪些需要建立?哪些需要更新?
#!/usr/bin/python# -*- coding:utf-8 -*-old_dict = { "#1": {‘hostname‘: ‘c1‘, ‘cpu_count‘: 2, ‘mem_capicity‘: 80}, "#2": {‘hostname‘: ‘c1‘, ‘cpu_count‘: 2, ‘mem_capicity‘: 80}, "#3": {‘hostname‘: ‘c1‘, ‘cpu_count‘: 2, ‘mem_capicity‘: 80},}new_dict = { "#1": {‘hostname‘: ‘c1‘, ‘cpu_count‘: 2, ‘mem_capicity‘: 800}, "#3": {‘hostname‘: ‘c1‘, ‘cpu_count‘: 2, ‘mem_capicity‘: 80}, "#4": {‘hostname‘: ‘c2‘, ‘cpu_count‘: 2, ‘mem_capicity‘: 80},}new_set = set()old_set = set()for i in new_dict: new_set.add(i)for j in old_dict: old_set.add(j)new_add = new_set.difference(old_set) #new_dict中有,old_dict中沒有old_del = old_set.difference(new_set) #old_dict中有,new_dict中沒有update = new_set.intersection(old_set) #old_dict和new_dict共同有的,需要把new_dict更新到old_dict中for k in new_add: old_dict[k] = new_dict[k] #將new_dict中新增的內容添加到old_dict中for v in old_del: del old_dict[v] #將old_dict中失效的內容刪除for m in update: old_dict[m] = new_dict[m] #把new_dict更新到old_dict中print(old_dict)
python基礎資料型別 (Elementary Data Type)——set