zoukankan      html  css  js  c++  java
  • Python-集合的常用操作

     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 # Author:Irving
     4 list_1 = [1,4,5,7,3,6,9,7]
     5 list_1 = set(list_1)
     6 
     7 list_2 = set([2,6,0,66,22,8,4])
     8 print(list_1,list_2)
     9 
    10 #交集
    11 print( list_1.intersection(list_2))
    12 
    13 #并集
    14 print(list_1.union(list_2))
    15 
    16 #差集 in list_1 but not in list_2
    17 print(list_1.difference(list_2))
    18 
    19 #判断子集
    20 list_3 = set([1,3,7])
    21 print(list_3.issubset(list_1))
    22 print(list_1.issuperset(list_3)) #父集
    23 
    24 #对称差集(把互相没有的取出来)
    25 print(list_1.symmetric_difference(list_2))
    26 
    27 #add
    28 list_1.add(999)
    29 print(list_1)
    30 
    31 #update
    32 list_1.update([888,777,555])
    33 print(list_1)
    34 
    35 print(list_1.pop())
    36 print(list_1.pop())
    37 print(list_1.pop())
    38 print(list_1.pop())
    39 print(list_1.pop())
    40 
    41 #discard删除元素不存在不会报错,remove则会
    42 list_1.discard(7)
  • 相关阅读:
    zoj 1239 Hanoi Tower Troubles Again!
    zoj 1221 Risk
    uva 10192 Vacation
    uva 10066 The Twin Towers
    uva 531 Compromise
    uva 103 Stacking Boxes
    稳定婚姻模型
    Ants UVA
    Golden Tiger Claw UVA
    关于upper、lower bound 的探讨
  • 原文地址:https://www.cnblogs.com/Xuuuuuu/p/9374470.html
Copyright © 2011-2022 走看看