Python學習——set集合的補充

來源:互聯網
上載者:User

標籤:hid   color   date()   section   包含   car   cti   alt   不重複   

set 是一個無序且不重複的元素集合
>>> num = {1,2,3,4,5}

1.add()添加一個元素
>>> num.add(6)
>>> num
>>> {1,2,3,4,5,6}

2.clear()清除集合中所有元素
>>> num.clear()
>>> num
>>> set()

3.copy()複製一個集合
>>> num1 = num.copy()
>>> num1
>>> {1,2,3,4,5}

4.difference()取得集合在一個或多個集合中不同的元素
>>> num1 = {2,4,6,8,10}
>>> num2 = {1,3,11,12,14}
#返回在一個集合中不同的元素
>>> num.difference(num1)
>>> {1,3,5}
#返回在多個集合中不同的元素
>>> num.difference(num1,num2)
>>> {5}

5.difference_update()刪除當前集合中所有包含在新集合裡的元素
>>> num1 = {2,4,6,8,10}
>>> num2 = {1,2,11,13}
>>> num.difference_update(num1,num2)
>>> num
>>> {3,5}

6.discard()從集合中移除一個元素,如果元素不存在,不做任何處理
>>> num.discard(1)
>>> num
>>> {2,3,4,5}

7.intersection()取交集,建立一個集合
>>> num1 ={1,3,5,7,9}
>>> num.intersection(num1)
>>> {1,3,5}

8.intersection_update()取交集,修改與原來的集合
>>> num1 = {1,3,5,7,9}
>>> num.intersection_update(num1)
>>> num
>>> {1,3,5}

9.isdisjoint()如果沒有交集,返回True
>>> num2 ={6,8,10}
>>> num.isdisjoint(num2)
>>> True

10.pop()從集合開頭移除一個元素
>>> num.pop()
>>> 1
>>> num
>>> {2,3,4,5}
PS:如果集合為空白,返回錯誤提示

11.symmetric_difference()差集,建立新對象
>>> num = {1,2,3,4,5,6}
>>> num1 = {2,3,4,6,8,9}
>>> num.symmetric_difference(num1)
>>> {1,5,8,9}

12.symmetric_difference_update()差集,改變原來的集合
>>> num = {1,2,3,4,5,6}
>>> num1 = {2,3,4,6,8,9}
>>> num.symmetric_difference_update(num1)
>>> num
>>> {1,5,8,9}

13.union()並集,返回一個新集合
>>> num ={1,2,4,6,7}
>>> num1 ={,2,4,6,8,10,12}
>>> num.union(num1)
>>> {1,2,4,6,7,8,10,12}

14.update()並集,並更新該集合
>>> num ={1,2,4,6,7}
>>> num1 ={,2,4,6,8,10,12}
>>> num.update(num1)
>>> num
>>> {1,2,4,6,7,8,10,12}

小練習:
 1 old_dict = { 2     "#1": {‘hostname‘: ‘c1‘, ‘cpu_count‘: 2, ‘mem_capicity‘: 80}, 3     "#2": {‘hostname‘: ‘c1‘, ‘cpu_count‘: 2, ‘mem_capicity‘: 80}, 4     "#3": {‘hostname‘: ‘c1‘, ‘cpu_count‘: 2, ‘mem_capicity‘: 80}      5 } 6 new_dict = { 7     "#1": {‘hostname‘: ‘c1‘, ‘cpu_count‘: 2, ‘mem_capicity‘: 800}, 8     "#3": {‘hostname‘: ‘c1‘, ‘cpu_count‘: 2, ‘mem_capicity‘: 80}, 9     "#4": {‘hostname‘: ‘c2‘, ‘cpu_count‘: 2, ‘mem_capicity‘: 80}     10 }11 old_set = set(old_dict.keys()) 12 update_list = list(old_set.intersection(new_dict.keys()))13 14 new_list = [] 15 del_list = []16 17 for i in new_dict.keys():     18   if i not in update_list:         19     new_list.append(i)20 for i in old_dict.keys():     21   if i not in update_list:         22     del_list.append(i)23 print (update_list,new_list,del_list,new_dict.keys()) 24 print(new_dict)
View Code

結果為:
>>> [‘#1‘, ‘#3‘] [‘#4‘] [‘#2‘] dict_keys([‘#1‘, ‘#3‘, ‘#4‘])
>>>{
‘#1‘: {‘mem_capicity‘: 800, ‘hostname‘: ‘c1‘, ‘cpu_count‘: 2},
‘#3‘: {‘mem_capicity‘: 80, ‘hostname‘: ‘c1‘, ‘cpu_count‘: 2},
‘#4‘: {‘mem_capicity‘: 80, ‘hostname‘: ‘c2‘, ‘cpu_count‘: 2}
}

Python學習——set集合的補充

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.