【4】python_基础_集合、json模块、函数

1 集合(天生去重)

 1.1 定义集合

              集合是无序的

  

 1 # 定义集合:天生去重
 2 nums = [1,1,2,3,4,4]
 3 num_set = set (nums)    # 1、使用set将其他类型转成集合(去重)
 4 print('1======',num_set)      #   {1, 2, 3, 4}
 5 
 6 nums2 = {1,1,2,3,4,4}   # 2、直接定义集合
 7 print ('2======',nums2)    #   {1, 2, 3, 4}
 8 
 9 nums3 = set()   # 3、定义空集合,不能写成num3={}-定义空字典
10 print ('3======',nums3)
1 1 1====== {1, 2, 3, 4}
2 2 2====== {1, 2, 3, 4}
3 3 3====== set()
View Code

1.2 集合取值

1 1 # 集合取值
2 2 for n in nums2:
3 3     print('4======',n)
View Code
1 1 4====== 1
2 2 4====== 2
3 3 4====== 3
4 4 4====== 4
View Code

1.3 集合关系

    

  • 交集  
    • set1.intersection(set2)       取set1及set2中都有的
    • set1 & set2
  • 并集  
    • set1.union(set2)  取set1合并set2
    • set1 | set2  
  • 差集
    • set1.difference(set2)  取set1中有的,set2中没有的
    • set1 - set2
  • 对称差集
    • set1.symmetric_difference(set2)  取 (set1 - set2) | (set2-set1)
    • set1 ^ set2
  • 父集、子集
    • set2.issubset(sest1)  set2是set1的子集?如果是,返回True;如果不是,返回False
    • set1.issuperset(set2)  set1是set2的父集?如果是,返回True;如果不是,返回False
       1 1 # http://www.nnzhp.cn/archives/160
       2  2 # 集合作用
       3  3     # 集合是无序的,不能通过下标取值
       4  4     #  1、去重
       5  5     # 2、关系测试-交集,差集,并集,反向差集/对称差集 (eg.list1、list2 数据处理)
       6  6 list =[1,2,3,4,5,3,6]
       7  7 nums={2,3,4,3}  #集合是无序的,不能通过下标取值,可以循环
       8  8 list_2 =[2,3,5,7,8]
       9  9 list_3 =[1,3,6]
      10 10 
      11 11 #定义集合
      12 12 list = set(list)
      13 13 list_2 = set (list_2)
      14 14 list_3 = set (list_3)
      15 15 
      16 16 print('1=====',list.intersection(list_2))    # 交集 (取list中且list2中有的)   {2, 3, 5}
      17 17 print('2=====',list & list_2)    # 交集 (取list中且list2中有的)   {2, 3, 5}
      18 18 
      19 19 print('3=====',list.union(list_2))   # 并集(去重统一展示)    {1, 2, 3, 4, 5, 6, 7, 8}
      20 20 print('4=====',list | list_2) # 并集(去重统一展示)    {1, 2, 3, 4, 5, 6, 7, 8}
      21 21 
      22 22 print('5=====',list.difference(list_2))  # 差集 (取list中有的,list2中没有的。即属于list且不属于list2的元素集合)    {1, 4, 6}
      23 23 print('6=====',list_2.difference(list))  # 差集 (取list_2中有的,list中没有的)   {8, 7}
      24 24 print('7=====',list -list_2)  # 差集 (取list中有的,list2中没有的)    {1, 4, 6}
      25 25 
      26 26 import string
      27 27 all_nums = set(string.digits)
      28 28 print('8=====',all_numbs)
      29 29 num4 = {'1','2','3'}
      30 30 print('9=====',all_nums.issuperset(num4))
      31 31 
      32 32 print('10=====',list_3.issubset(list))    #返回布尔值,如果list_3是list的子集(list_3的值,在list中都有),返回True
      33 33 print('11=====',list.issuperset(list_3))  #返回布尔值,如果list是list_3的父集(list_3的值,在list中都有),返回True
      34 34 
      35 35 print('12=====',list.symmetric_difference(list_2))    #对称差集   (取"list中有的,list2中没有的" 并上 "list_2中有的,list中没有的"  。即集合list与集合list2中所有不属于list∩list2的元素的集合。即把2集合中都有的去掉){1, 4, 6, 7, 8}
      36 36 print('13=====',list ^ list_2)    #对称差集   (取"list中有的,list2中没有的" 并上 "list_2中有的,list中没有的"  去重){1, 4, 6, 7, 8}
      View Code
       1 1 1===== {2, 3, 5}
       2  2 2===== {2, 3, 5}
       3  3 3===== {1, 2, 3, 4, 5, 6, 7, 8}
       4  4 4===== {1, 2, 3, 4, 5, 6, 7, 8}
       5  5 5===== {1, 4, 6}
       6  6 6===== {8, 7}
       7  7 7===== {1, 4, 6}
       8  8 8===== {'1', '7', '4', '0', '2', '6', '8', '9', '3', '5'}
       9  9 9===== True
      10 10 10===== True
      11 11 11===== True
      12 12 12===== {1, 4, 6, 7, 8}
      13 13 13===== {1, 4, 6, 7, 8}
      View Result

密码较验练习

 1 1 #!/usr/bin/python
 2  2 # -*- coding:utf-8 -*-
 3  3 # Author:hwm
 4  4 
 5  5 #密码较验:必须包含大小写字母、数字、特殊字符
 6  6 import string
 7  7 letters = set (string.ascii_letters)
 8  8 digits = set (string.digits)
 9  9 pun = set (string.punctuation)
10 10 
11 11 # all = letters| digits| pun
12 12 #if pwd.issubset(all):
13 13 for i in range(5):
14 14     pwd = input('passwd:').strip()
15 15     pwd = set(pwd)
16 16     if pwd & letters and pwd & digits and pwd & pun:
17 17         print('密码合法')
18 18     else:
19 19         print('密码不合法,密码必须包含大小写字母、数字、特殊字符')
View Code
1 1 passwd:1
2 2 密码不合法,密码必须包含大小写字母、数字、特殊字符
3 3 passwd:abc1a
4 4 密码不合法,密码必须包含大小写字母、数字、特殊字符
5 5 passwd:abc1e)
6 6 密码合法
7 7 passwd:
View Result

1.4 集合操作方法

    

  • 添加  
    • add(ele)  一次添加一个
    • update(list)  一次添加多个
  • 删除
    • pop()  随机删除一个元素。
    • discard(ele)  删除元素。如果元素不存在,不报错,返回None。推荐使用。
    • remove(ele)    删除元素。如果元素不存在,报错。不推荐使用

  

 1  1 print('-------------集合操作-------------')
 2  2 print('-------------添加-------------')
 3  3 list.add(777)   # 一次添加一个
 4  4 print('14=====',list)
 5  5 list.update([888,999]) # 同时添加多个
 6  6 print('15=====',list)
 7  7 print('-------------删除-------------')
 8  8 list.remove(999)    # 删除一个不存在的元素,报错
 9  9 print('16=====',list)
10 10 list.pop()  # 随机删除集合中的一个元素
11 11 print('17=====',list)
12 12 list.discard(888)
13 13 print('18=====',list)        # 删除一个不存在的元素,不报错
View Code
1 1 -------------集合操作-------------
2 2 -------------添加-------------
3 3 14===== {1, 2, 3, 4, 5, 6, 777}
4 4 15===== {1, 2, 3, 4, 5, 6, 999, 777, 888}
5 5 -------------删除-------------
6 6 16===== {1, 2, 3, 4, 5, 6, 777, 888}
7 7 17===== {2, 3, 4, 5, 6, 777, 888}
8 8 18===== {2, 3, 4, 5, 6, 777}
View Result

猜你喜欢

转载自www.cnblogs.com/xhybk/p/9258949.html