zoukankan      html  css  js  c++  java
  • Python入门篇-封装与解构和高级数据类型集合(set)和字典(dict)

            Python入门篇-封装与解构和高级数据类型集合(set)和字典(dict)

                                              作者:尹正杰

    版权声明:原创作品,谢绝转载!否则将追究法律责任。

    一.封装和结构

     1 #!/usr/bin/env python
     2 #_*_conding:utf-8_*_
     3 #@author :yinzhengjie
     4 #blog:http://www.cnblogs.com/yinzhengjie
     5 
     6 
     7 
     8 x = 1,3,5,7                              #Python中的封装,注意,这里把等号后边的int数据类型封装成元组了
     9 print(x)
    10 
    11 a,b = [100,200]                          #线性结构,可以同时为多个变量赋值
    12 a,b = b,a                                #可以很轻松的实现数据交换
    13 print(a,b)
    14 
    15 m,n = {"name":"Jason","Age":18}           #非线性结构也可以解构,即2边的个数要相同,m和n这2个变量分别赋值
    16 print(m,n)
    17 
    18 y,*z = 100,200,300,400,500,600           #注意,y为变量标识符,而z则为可变参数(可变参数只能存在一个,存在2个会报语法错误),他会把后面的元素放入到一个列表中
    19 print(y,z)
    20 
    21 
    22 head,*mid,tail = range(10)              #注意,这也是结构,左边的标识符要大于或等于2个才能算得上结构哟~
    23 print(head,mid,tail)
    24 
    25 x,[a,b],z = (1,[2,3],(4,5))             #当然,咱们也可以这样去解构,它会按照我们的想法对号入座
    26 print(x,a,b,z)
    27 
    28 
    29 
    30 #以上代码执行结果如下:
    31 (1, 3, 5, 7)
    32 200 100
    33 name Age
    34 100 [200, 300, 400, 500, 600]
    35 0 [1, 2, 3, 4, 5, 6, 7, 8] 9
    36 1 2 3 (4, 5)

    二.集合(set)

    1>.集合的特点

    约定
      set 翻译为集合
      collection 翻译为集合类型或容器,是一个大概念
    
    set   可变的、无序的、不重复的元素的集合

    2>.set定义和初始化

    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    '''
      set() -> new empty set object
      set(iterable) -> new set object
    '''
    
    s1 = set()
    
    s2 = set(range(10))
    
    s3 = set(list(range(20)))
    
    #dict
    s4 = {}
    
    #set
    s5 = {1,3,5,7,9}
    
    s6 = {(1,3),5,'A'}
    
    #集合只能存放不可变的的元素,如果存放list和bytearray时会报错:"unhashable type"
    s7 = {(2,),3,None,"abc",b"ABC"}
    
    s8 = set(enumerate(range(5)))
    
    print(s1)
    print(s2)
    print(s3)
    print(s4)
    print(s5)
    print(s6)
    print(s7)
    print(s8)
    
    
    
    #以上代码执行结果如下:
    set()
    {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
    {}
    {1, 3, 5, 7, 9}
    {'A', (1, 3), 5}
    {3, None, (2,), b'ABC', 'abc'}
    {(0, 0), (3, 3), (4, 4), (2, 2), (1, 1)}

    3>.set的元素

    set的元素要求必须可以hash
    
    不可hash的类型有list、set,bytearray,dict
    
    元素不可以索引
    
    set可以迭代

    4>.set增加

    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    '''
    add(elem)
        增加一个元素到set中
        如果元素存在,什么都不做
    '''
    
    s1 = {1,3,5}
    print(s1)
    
    s1.add(100)
    print(s1)
    
    
    
    #以上代码执行结果如下:
    {1, 3, 5}
    {1, 3, 100, 5}
    add(elem)         #增加一个元素到set中,如果元素存在,什么都不做
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    '''
    update(*others)
        合并其他元素到set集合中来
        参数others必须是可迭代对象
        就地修改
    '''
    
    s1 = {1,3,5}
    print(s1,id(s1))
    
    s1.update([1,3,2],[2,3,4],(6,8))
    print(s1,id(s1))
    
    
    
    #以上代码执行结果如下:
    {1, 3, 5} 31487144
    {1, 2, 3, 4, 5, 6, 8} 31487144
    update(*others)     #合并其他元素到set集合中来,参数others必须是可迭代对象,就地修改

    5>.set删除

    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    '''
    remove(elem)
        从set中移除一个元素
        元素不存在,抛出KeyError异常。为什么是KeyError?因为这个key对应的是一个hash值。
    '''
    
    s1 = {1,3,5}
    print(s1)
    
    s1.remove(3)
    print(s1)
    
    
    
    #以上代码执行结果如下:
    {1, 3, 5}
    {1, 5}
    remove(elem)       #从set中移除一个元素,元素不存在,抛出KeyError异常
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    '''
    pop() -> item
        移除并返回任意的元素。为什么是任意元素?
        空集返回KeyError异常
    '''
    
    s1 = {1,3,5}
    print(s1)
    
    s1.pop()
    print(s1)
    
    
    
    #以上代码执行结果如下:
    {1, 3, 5}
    {3, 5}
    pop() -> item      #移除并返回任意的元素。为什么是任意元素? 空集返回KeyError异常
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    '''
    discard(elem)
         从set中移除一个元素
        元素不存在,什么都不做
    '''
    
    s1 = {1,3,5}
    print(s1)
    
    s1.discard(3)
    print(s1)
    
    
    
    #以上代码执行结果如下:
    {1, 3, 5}
    {1, 5}
    discard(elem)      #从set中移除一个元素,元素不存在,什么都不做,即不出错版本的remove
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    '''
    clear()
        移除所有元素
    '''
    
    s1 = {1,3,5}
    print(s1)
    
    s1.clear()
    print(s1)
    
    
    
    #以上代码执行结果如下:
    {1, 3, 5}
    set()
    clear()          #移除所有元素

    6>.set修改、查询

    修改
      要么删除,要么加入新的元素
      为什么没有修改?
    
    索引   非线性结构,无法索引,一般只有线性结构才会存在索引的概念。

    查询
      线性结构查询时需要遍历整个数据结构,时间复杂度为0(n);而非线性结构比如Set和Dict在查询时会先求一个hash值,然后根据这个哈希值去相应的hash表中查找对应的数据,时间复杂度为O(1)。
    遍历   可以迭代所有元素,不管是线性结构还是非线性结构遍历的速度都会很慢。因为时间复杂度都一样,遍历的时候只跟数据的规模有关。
    成员运算符   
    in 和not in 判断元素是否在set中   效率要比线性结构数据要高,时间复杂度为O(1)。

    7>.set成员运算符的比较

    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    import datetime
    
    #注意,这个值我们可以调大里面的数字进行测试,如果在后面再加一个0就会把内存吃满。
    list_1 = list(range(100000000))
    start = datetime.datetime.now()
    a = -1 in list_1
    end = datetime.datetime.now()
    
    print("在列表中遍历一个数字耗时为:{}".format(end - start))
    
    set1 = set(range(100000000))
    start = datetime.datetime.now()
    a = -1 in set1
    end = datetime.datetime.now()
    print("在集合中遍历一个数字耗时为:{}".format(end - start))
    
    
    
    #以上代码执行结果如下:
    在列表中遍历一个数字耗时为:0:00:01.236070
    在集合中遍历一个数字耗时为:0:00:00

    8>.set和线性结构

      线性结构的查询时间复杂度是O(n),即随着数据规模的增大而增加耗时
      set、dict等结构,内部使用hash值作为key,时间复杂度可以做到O(1),查询时间和数据规模无关
      
      可hash     数值型int、float、complex     布尔型True、False     字符串string、bytes     tuple     None     以上都是不可变类型,称为可哈希类型,hashable。当然还有其它自定义类型也可实现hash,我们暂时先不考虑。
      set的元素必须是可hash的

    9>.集合运算

    基本概念
      全集
        所有元素的集合。例如实数集,所有实数组成的集合就是全集
      子集subset和超集superset
        一个集合A所有元素都在另一个集合B内,A是B的子集,B是A的超集
      真子集和真超集
        A是B的子集,且A不等于B,A就是B的真子集,B是A的真超集
      并集:多个集合合并的结果
      交集:多个集合的公共部分
      差集:集合中除去和其他集合公共部分
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    '''
    并集
        将两个集合A和B的所有的元素合并到一起,组成的集合称作集合A与集合B的并集
        union(*others)
            返回和多个集合合并后的新的集合
        | 运算符重载
            等同union
        update(*others)
            和多个集合合并,就地修改
        |=
            等同update
    '''
    
    s1 = {1,3,5}
    s2 = {2,4,6}
    print("s1 = {}".format(s1))
    print("s2 = {}".format(s2))
    
    s3 =s1.union(s2)
    print("s3 = {}".format(s3))
    
    s4 = s1 | s2
    print("s4 = {}".format(s4))
    
    s1.update(s2)
    print("s1 = {}".format(s1))
    print("s2 = {}".format(s2))
    
    s2 |= s1
    print("s1 = {}".format(s1))
    print("s2 = {}".format(s2))
    
    
    
    
    #以上代码执行结果如下:
    s1 = {1, 3, 5}
    s2 = {2, 4, 6}
    s3 = {1, 2, 3, 4, 5, 6}
    s4 = {1, 2, 3, 4, 5, 6}
    s1 = {1, 2, 3, 4, 5, 6}
    s2 = {2, 4, 6}
    s1 = {1, 2, 3, 4, 5, 6}
    s2 = {1, 2, 3, 4, 5, 6}
    并集:将两个集合A和B的所有的元素合并到一起,组成的集合称作集合A与集合B的并集
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    '''
    交集
        集合A和B,由所有属于A且属于B的元素组成的集合
        intersection(*others)
            返回和多个集合的交集
        &
            等同intersection
        intersection_update(*others)
            获取和多个集合的交集,并就地修改
        &=
            等同intersection_update
    '''
    
    s1 = {1,3,5}
    s2 = {1,2,3,4,5,6}
    print("s1 = {}".format(s1))
    print("s2 = {}".format(s2))
    
    s3 = s1.intersection(s2)
    print("s3 = {}".format(s3))
    
    s4 = s1 & s2
    print("s4 = {}".format(s4))
    
    s1.intersection_update(s2)
    print("s1 = {}".format(s1))
    print("s2 = {}".format(s2))
    
    s2 &= s1
    print("s1 = {}".format(s1))
    print("s2 = {}".format(s2))
    
    
    
    #以上代码执行结果如下:
    s1 = {1, 3, 5}
    s2 = {1, 2, 3, 4, 5, 6}
    s3 = {1, 3, 5}
    s4 = {1, 3, 5}
    s1 = {1, 3, 5}
    s2 = {1, 2, 3, 4, 5, 6}
    s1 = {1, 3, 5}
    s2 = {1, 3, 5}
    交集:集合A和B,由所有属于A且属于B的元素组成的集合
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    '''
    差集
        集合A和B,由所有属于A且不属于B的元素组成的集合
        difference(*others)
            返回和多个集合的差集
        -
            等同difference
        difference_update(*others)
            获取和多个集合的差集并就地修改
        -=
            等同difference_update
    '''
    
    s1 = {1,3,5}
    s2 = {1,2,3,4,5,6,7,8,9}
    print("s1 = {}".format(s1))
    print("s2 = {}".format(s2))
    
    s3 = s2.difference(s1)
    print("s3 = {}".format(s3))
    
    s2.difference_update(s1)
    print("s1 = {}".format(s1))
    print("s2 = {}".format(s2))
    
    s4 = {1,3,5,100,200,300}
    s1 -= s4
    print("s1 = {}".format(s1))
    print("s2 = {}".format(s2))
    print("s4 = {}".format(s4))
    
    
    
    #以上代码执行结果如下:
    s1 = {1, 3, 5}
    s2 = {1, 2, 3, 4, 5, 6, 7, 8, 9}
    s3 = {2, 4, 6, 7, 8, 9}
    s1 = {1, 3, 5}
    s2 = {2, 4, 6, 7, 8, 9}
    s1 = set()
    s2 = {2, 4, 6, 7, 8, 9}
    s4 = {1, 3, 100, 5, 200, 300}
    差集:集合A和B,由所有属于A且不属于B的元素组成的集合
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    '''
    对称差集
        集合A和B,由所有不属于A和B的交集元素组成的集合,记作(A-B)∪(B-A)
        symmetric_differece(other)
        返回和另一个集合的差集
        ^
            等同symmetric_differece
        symmetric_differece_update(other)
            获取和另一个集合的差集并就地修改
        ^=
            等同symmetric_differece_update
    '''
    
    s1 = {1,3,5}
    s2 = {100,200,300}
    print("s1 = {}".format(s1))
    print("s2 = {}".format(s2))
    
    s3 = s1.symmetric_difference(s2)
    print("s3 = {}".format(s3))
    
    s4 = s1 ^ s2
    print("s4 = {}".format(s4))
    
    
    s1.symmetric_difference_update(s2)
    print("s1 = {}".format(s1))
    print("s2 = {}".format(s2))
    
    s2 ^= s1
    print("s1 = {}".format(s1))
    print("s2 = {}".format(s2))
    
    
    
    #以上代码执行结果如下:
    s1 = {1, 3, 5}
    s2 = {200, 100, 300}
    s3 = {1, 3, 100, 5, 200, 300}
    s4 = {1, 3, 100, 5, 200, 300}
    s1 = {1, 3, 100, 5, 200, 300}
    s2 = {200, 100, 300}
    s1 = {1, 3, 100, 5, 200, 300}
    s2 = {1, 3, 5}
    对称差集:集合A和B,由所有不属于A和B的交集元素组成的集合,记作(A-B)∪(B-A)

    10>.集合运算

    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    '''
    issubset(other)、<=
        判断当前集合是否是另一个集合的子集
    '''
    
    s1 = {1,3,5}
    s2 = {1,3,5,7,9}
    print("s1 = {}".format(s1))
    print("s2 = {}".format(s2))
    
    #判断当前集合是否是另一个集合的子集
    print(s1.issubset(s2))
    
    #判断s2是否是s1的真子集
    print(s2 < s1)
    
    #判断当前集合是否是other的超集
    print(s2.issuperset(s1))
    print(s2 >= s1)
    
    #判断s2是否是s1的真超集
    print(s2 > s1)
    
    #当前集合和另一个集合没有交集,没有交集,则返回True
    print(s2.isdisjoint(s1))
    
    
    
    #以上代码执行结果如下:
    s1 = {1, 3, 5}
    s2 = {1, 3, 5, 7, 9}
    True
    False
    True
    True
    True
    False

    11>.集合应用

    共同好友
      你的好友A、B、C,他的好友C、B、D,求共同好友
      交集问题:{'A', 'B', 'C'}.intersection({'B', 'C', 'D'})
    
    微信群提醒
      XXX与群里其他人都不是微信朋友关系
      并集:userid in (A | B | C | ...) == False,A、B、C等是微信好友的并集,用户ID不在这个并集中,说明他和任何人都不是朋友(这种方法时可以实现的但是可能会存在占用过多的内存空间)
      群里所有其他人IDs都不在X的朋友列表T中。  T & IDs == set() 权限判断   有一个API,要求权限同时具备A、B、C才能访问,用户权限是B、C、D,判断用户是否能够访问该API       API集合A,权限集合P       A
    - P = {} ,A-P为空集,说明P包含A       A.issubset(P) 也行,A是P的子集也行       A & P = A 也行   有一个API,要求权限具备A、B、C任意一项就可访问,用户权限是B、C、D,判断用户是否能够访问该API       API集合A,权限集合P       A & P != {} 就可以       A.isdisjoint(P) == False 表示有交集 一个总任务列表,存储所有任务。一个完成的任务列表。找出为未完成的任务   业务中,任务ID一般不可以重复   所有任务ID放到一个set中,假设为ALL   所有已完成的任务ID放到一个set中,假设为COMPLETED,它是ALL的子集   ALL - COMPLETED = UNCOMPLETED

    12>.集合练习

    随机产生2组各10个数字的列表,如下要求:
      每个数字取值范围[10,20]
      统计20个数字中,一共有多少个不同的数字?
      2组中,不重复的数字有几个?分别是什么?
      2组中,重复的数字有几个?分别是什么?
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    
    a = [1, 9, 7, 5, 6, 7, 8, 8, 2, 6]
    b = [1, 9, 0, 5, 6, 4, 8, 3, 2, 3]
    s1 = set(a)
    s2 = set(b)
    print(s1)
    print(s2)
    print(s1.union(s2))
    print(s1.symmetric_difference(s2))
    print(s1.intersection(s2))
    
    
    
    #以上代码执行结果如下:
    {1, 2, 5, 6, 7, 8, 9}
    {0, 1, 2, 3, 4, 5, 6, 8, 9}
    {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    {0, 3, 4, 7}
    {1, 2, 5, 6, 8, 9}
    参考案例 

    三.字典(dict)

    1>.字典特点

    key-value键值对的数据的集合
    
    可变的、无序的、key不重复

    2>.字典定义和初始化

    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    #定义一个空字典
    d1 = dict()
    
    #和上面的方法一致,也是定义了一个空字典
    d2 = {}
    
    #dict(**kwargs) 使用name=value对初始化一个字典
    d3 = dict(a=10,b=20,c=[123])
    
    #dict(iterable, **kwarg) 使用可迭代对象和name=value对构造字典,不过可迭代对象的元素必须是一个二元结构
    d4 = dict(((1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e')))
    d5 = dict(([1,'a'],[2,'b'],[3,'c'],[4,'d'],(5,'e')))
    
    #dict(mapping, **kwarg) 使用一个字典构建另一个字典
    d6 = {'a':10, 'b':20, 'c':None, 'd':[1,2,3]}
    
    #类方法dict.fromkeys(iterable, value)
    d7 = dict.fromkeys(range(5))
    d8 = dict.fromkeys(range(5),666)
    
    
    print("d1 = {}".format(d1))
    print("d2 = {}".format(d2))
    print("d3 = {}".format(d3))
    print("d4 = {}".format(d4))
    print("d5 = {}".format(d5))
    print("d6 = {}".format(d6))
    print("d7 = {}".format(d7))
    print("d8 = {}".format(d8))
    
    
    
    #以上代码输出结果如下:
    d1 = {}
    d2 = {}
    d3 = {'a': 10, 'b': 20, 'c': [123]}
    d4 = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
    d5 = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
    d6 = {'a': 10, 'b': 20, 'c': None, 'd': [1, 2, 3]}
    d7 = {0: None, 1: None, 2: None, 3: None, 4: None}
    d8 = {0: 666, 1: 666, 2: 666, 3: 666, 4: 666}

    3>.字典元素的访问

    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    '''
    d[key]
        返回key对应的值value
        key不存在抛出KeyError异常
    '''
    
    d1 = {'Name': "Jason Yin", 'Age': 18, 'Hobby': ["跑步","旅行","写博客"]}
    
    print("d1 = {}".format(d1))
    print("姓名:{},年龄:{},爱好:{}".format(d1["Name"],d1["Age"],d1["Hobby"]))
    
    
    
    #以上代码输出结果如下:
    d1 = {'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    姓名:Jason Yin,年龄:18,爱好:['跑步', '旅行', '写博客']
    d[key]               #返回key对应的值value key不存在抛出KeyError异常
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    '''
    get(key[, default])
        返回key对应的值value
        key不存在返回缺省值,如果没有设置缺省值就返回None
    '''
    
    d1 = {'Name': "Jason Yin", 'Age': 18, 'Hobby': ["跑步","旅行","写博客"]}
    
    
    print(d1.get("Name"))
    
    print(d1.get("forte"))
    print(d1.get("forte","该key不存在!"))
    
    
    #以上代码执行结果如下:
    Jason Yin
    None
    该key不存在!
    get(key[, default])       #返回key对应的值value key不存在返回缺省值,如果没有设置缺省值就返回None
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    '''
    setdefault(key[, default])
        返回key对应的值value
        key不存在,添加kv对,value为default,并返回default,如果default没有设置,缺省为None
    '''
    
    d1 = {'Name': "Jason Yin", 'Age': 18, 'Hobby': ["跑步","旅行","写博客"]}
    
    print("d1={}".format(d1))
    
    print(d1.setdefault("Name","Yinzhengjie"))
    
    print(d1.setdefault("forte","大数据运维开发"))
    
    print("d1={}".format(d1))
    
    
    
    #以上代码执行结果如下:
    d1={'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    Jason Yin
    大数据运维开发
    d1={'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客'], 'forte': '大数据运维开发'}
    setdefault(key[, default])   #返回key对应的值value key不存在,添加kv对,value为default,并返回default,如果default没有设置,缺省为None

    4>.字典增加和修改

    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    '''
    d[key] = value
        将key对应的值修改为value
        key不存在添加新的kv对
    '''
    
    d1 = {'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    print("d1={}".format(d1))
    
    d1["Name"] = "Yinszhengjie"
    print("d1={}".format(d1))
    
    d1["forte"] = "大数据运维开发"
    print("d1={}".format(d1))
    
    
    #以上代码执行结果如下:
    d1={'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    d1={'Name': 'Yinszhengjie', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    d1={'Name': 'Yinszhengjie', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客'], 'forte': '大数据运维开发'}
    d[key] = value          #将key对应的值修改为value key不存在添加新的kv对
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    '''
    update([other]) -> None
        使用另一个字典的kv对更新本字典
        key不存在,就添加
        key存在,覆盖已经存在的key对应的值
        就地修改
    '''
    
    d1 = {'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    print("d1={}".format(d1))
    
    d1.update(Age=20)
    print("d1={}".format(d1))
    
    d1.update((('Age',24),))
    print("d1={}".format(d1))
    
    d1.update({"Age":26})
    print("d1={}".format(d1))
    
    
    #以上代码执行结果如下:
    d1={'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    d1={'Name': 'Jason Yin', 'Age': 20, 'Hobby': ['跑步', '旅行', '写博客']}
    d1={'Name': 'Jason Yin', 'Age': 24, 'Hobby': ['跑步', '旅行', '写博客']}
    d1={'Name': 'Jason Yin', 'Age': 26, 'Hobby': ['跑步', '旅行', '写博客']}
    update([other]) -> None     #使用另一个字典的kv对更新本字典 key不存在,就添加 key存在,覆盖已经存在的key对应的值 就地修改

    5>.字典删除

    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    '''
    pop(key[, default])
        key存在,移除它,并返回它的value
        key不存在,返回给定的default
        default未设置,key不存在则抛出KeyError异常
    '''
    
    d1 = {'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    print("d1={}".format(d1))
    
    d1.pop("Hobby")
    print("d1={}".format(d1))
    
    
    
    #以上代码执行结果如下:
    d1={'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    d1={'Name': 'Jason Yin', 'Age': 18}
    pop(key[, default])       #key存在,移除它,并返回它的value key不存在,返回给定的default default未设置,key不存在则抛出KeyError异常
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    '''
    popitem()
        移除并返回一个任意的键值对
        字典为empty,抛出KeyError异常
    '''
    
    d1 = {'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    print("d1={}".format(d1))
    
    d1.popitem()
    print("d1={}".format(d1))
    
    
    
    #以上代码执行结果如下:
    d1={'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    d1={'Name': 'Jason Yin', 'Age': 18}
    popitem()             #移除并返回一个任意的键值对 字典为empty,抛出KeyError异常
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    '''
    clear()
        清空字典
    '''
    
    d1 = {'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    print("d1={}".format(d1))
    
    d1.clear()
    print("d1={}".format(d1))
    
    
    #以上代码执行结果如下:
    d1={'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    d1={}
    clear()              #清空字典
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    '''
        del语句
    '''
    
    a = True
    b = [6]
    d = {'a': 1, 'b': b, 'c': [1,3,5]}
    print("a = {}".format(a))
    print("b = {}".format(b))
    print("d = {}".format(d))
    
    #删除对象“a”
    del a
    
    #看着像删除了一个对象,本质上减少了一个对象的引用,del 实际上删除的是名称,而不是对象
    del d['c']
    del b[0]
    
    c = b
    del c
    del b
    b = d['b']
    
    # print("a = {}".format(a))
    print("b = {}".format(b))
    print("d = {}".format(d))
    # print("c = {}".format(c))
    
    
    #以上代码执行结果如下:
    a = True
    b = [6]
    d = {'a': 1, 'b': [6], 'c': [1, 3, 5]}
    b = []
    d = {'a': 1, 'b': []}
    del语句

    6>.字典遍历

    总结:
      Python3中,keys、values、items方法返回一个类似一个生成器的可迭代对象,不会把函数的返回结果复制到内存中
        它返回是一个Dictionary view对象,可以使用len(),iter(),in方法。需要注意的是,我们在遍历字典的时候不推荐对key的元素进行增删,因为你改动后会改变size的大小,如果强行执行key的删除会抛出异常的。
        字典的entry的动态的视图,字典变化,视图将反映出这些变化
        keys返回一个类set对象,也就是可以看做一个set集合。如果values都可以hash,那么items也可以看作是类set对象   Python2中,上面的方法会返回一个新的列表,占据新的内存空间。所以Python2建议使用iterkeys、itervalues、iteritems版本,返回一个迭代器,而不是一个copy。
    字典的key要求和set的元素要求一致:   set的元素可以就是看做key,set可以看做dict的简化版   hashable 可哈希才可以作为key,可以使用hash()测试   d
    = {1 : 0, 2.0 : 3, "abc" : None, ('hello', 'world', 'python') : "string", b'abc' : '135'}
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    d1 = {'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    
    
    for item in d1:
        print(item)
    
    print("*" * 20 + "我是分割线" + "*" * 20)
    for item in d1.keys():
        print(item)
    
    #以上代码执行结果如下:
    Name
    Age
    Hobby
    ********************我是分割线********************
    Name
    Age
    Hobby
    只遍历keys
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    d1 = {'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    
    
    for item in d1:
        print(d1[item])
    
    print("*" * 20 + "我是分割线" + "*" * 20)
    
    for item in d1.keys():
        print(d1.get(item))
    
    print("*" * 20 + "我是分割线" + "*" * 20)
    
    for item in d1.values():
        print(item)
    
    
    
    #以上代码执行结果如下:
    Jason Yin
    18
    ['跑步', '旅行', '写博客']
    ********************我是分割线********************
    Jason Yin
    18
    ['跑步', '旅行', '写博客']
    ********************我是分割线********************
    Jason Yin
    18
    ['跑步', '旅行', '写博客']
    只遍历values
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    d1 = {'Name': 'Jason Yin', 'Age': 18, 'Hobby': ['跑步', '旅行', '写博客']}
    
    
    for item in d1.items():
        print(item)
    
    print("*" * 20 + "我是分割线" + "*" * 20)
    
    for item in d1.items():
        print(item[0],item[1])
    
    print("*" * 20 + "我是分割线" + "*" * 20)
    
    for k,v in d1.items():
        print(k,v)
    
    print("*" * 20 + "我是分割线" + "*" * 20)
    
    for k,_ in d1.items():
        print(k)
    
    print("*" * 20 + "我是分割线" + "*" * 20)
    
    for _,v in d1.items():
        print(v)
    
    
    #以上代码执行结果如下:
    ('Name', 'Jason Yin')
    ('Age', 18)
    ('Hobby', ['跑步', '旅行', '写博客'])
    ********************我是分割线********************
    Name Jason Yin
    Age 18
    Hobby ['跑步', '旅行', '写博客']
    ********************我是分割线********************
    Name Jason Yin
    Age 18
    Hobby ['跑步', '旅行', '写博客']
    ********************我是分割线********************
    Name
    Age
    Hobby
    ********************我是分割线********************
    Jason Yin
    18
    ['跑步', '旅行', '写博客']
    遍历key和vlaue键值对

    7>.defaultdict

    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    """
    collections.defaultdict([default_factory[, ...]])
        第一个参数是default_factory,缺省是None,它提供一个初始化函数。当key不存在的时候,会调用这个工厂函数来生成key对应的value
    """
    
    
    from collections import defaultdict
    import random
    
    d1 = {}
    
    d2 = defaultdict(list)
    
    print("d1={}".format(d1))
    print("d2={}".format(d2))
    
    for k in "abcde":
        for v in range(random.randint(1,5)):
            if k not in d1.keys():
                d1[k] = []
            d1[k].append(v)
    
    print("d1={}".format(d1))
    
    for k in "mnopq":
        for v in range(random.randint(1,5)):
            d2[k].append(v)
    print("d2={}".format(d2))
    
    
    #以上代码输出结果如下所示:
    d1={}
    d2=defaultdict(<class 'list'>, {})
    d1={'a': [0, 1, 2], 'b': [0, 1, 2], 'c': [0, 1, 2, 3, 4], 'd': [0, 1, 2], 'e': [0]}
    d2=defaultdict(<class 'list'>, {'m': [0, 1, 2, 3], 'n': [0, 1, 2], 'o': [0, 1, 2], 'p': [0, 1, 2, 3, 4], 'q': [0, 1]})

    8>.orderedDict

    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    """
    collections.OrderedDict([items])
        key并不是按照加入的顺序排列,可以使用OrderedDict记录顺序
    
    
    有序字典可以记录元素插入的顺序,打印的时候也是按照这个顺序输出打印
    3.6版本的Python的字典就是记录key插入的顺序(IPython不一定有效果)
    
    
    应用场景:
        假如使用字典记录了N个产品,这些产品使用ID由小到大加入到字典中
        除了使用字典检索的遍历,有时候需要取出ID,但是希望是按照输入的顺序,因为输入顺序是有序的
        否则还需要重新把遍历到的值排序
    """
    
    
    from collections import OrderedDict
    import random
    
    d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2,'leechee':5}
    print("d = {}".format(d))
    
    keys = list(d.keys())
    random.shuffle(keys)
    print("keys = {}".format(keys))
    
    od = OrderedDict()
    for key in keys:
        od[key] = d[key]
    
    print("od = {}".format(od))
    print("od.keys() = {}".format(od.keys()))
    
    
    
    #以上代码执行结果如下:
    d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2, 'leechee': 5}
    keys = ['leechee', 'apple', 'pear', 'orange', 'banana']
    od = OrderedDict([('leechee', 5), ('apple', 4), ('pear', 1), ('orange', 2), ('banana', 3)])
    od.keys() = odict_keys(['leechee', 'apple', 'pear', 'orange', 'banana'])

    9>.字典小试牛刀

    需求一:用户输入一个数字
        打印每一位数字及其重复的次数
    
    需求二:数字重复统计     随机产生100个整数     数字的范围[
    -1000, 1000]     升序输出所有不同的数字及其重复的次数
    需求三:字符串重复统计     字符表
    'abcdefghijklmnopqrstuvwxyz'     随机挑选2个字母组成字符串,共挑选100个     降序输出所有不同的字符串及重复的次数
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    '''
    用户输入一个数字
        打印每一位数字及其重复的次数
    '''
    
    
    num = input("请输入一个整数:>>> ")
    
    d = {}
    
    for key in num:
        if not d.get(key):
            d[key] = 1
            continue
        d[key] += 1
    
    print(d)
    
    
    
    #以上代码执行结果如下:
    请输入一个整数:>>> 10086
    {'1': 1, '0': 2, '8': 1, '6': 1}
    需求一(解法一)
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    '''
    用户输入一个数字
        打印每一位数字及其重复的次数
    '''
    
    
    num = input("请输入一个整数:>>> ")
    
    d = {}
    
    for key in num:
        if not d.get(key):
            d[key] = 1
        else:
            d[key] += 1
            
    print(d)
    
    
    
    #以上代码执行结果如下:
    请输入一个整数:>>> 10086
    {'1': 1, '0': 2, '8': 1, '6': 1}
    需求一(解法二)
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    import random
    """
    数字重复统计
        随机产生100个整数
        数字的范围[-1000, 1000]
        升序输出所有不同的数字及其重复的次数
    """
    
    n = 100
    
    nums =  [0] * n
    
    d = {}
    
    for i in range(n):
        nums[i] = random.randint(-1000,1000)
    
    print(nums)
    
    t = nums.copy()
    t.sort()
    print(t)
    
    d = {}
    for x in nums:
        if x not  in d.keys():
            d[x] = 1
        else:
            d[x] += 1
    
    print(d)
    
    d1 = sorted(d.items())
    print(d1)
    
    
    
    
    #以上代码执行结果如下:
    [192, -254, 796, 920, -764, 28, -972, 48, -709, 613, 927, 191, -480, -461, 199, -686, -846, 448, -49, -588, 129, -295, 229, 433, 938, -363, -573, 181, 565, 906, 667, -615, 422, -614, -492, -373, 678, 123, 193, 308, -84, -493, 503, 203, -837, -386, -210, 897, 683, -206, -987, -660, -801, 402, 311, -562, -655, 936, 997, 892, -134, -664, -805, -573, 124, 399, 992, 999, -802, -17, -588, 11, 599, -221, -746, 199, 680, -276, 88, -945, -573, -29, -939, 181, 495, -763, -384, 916, 150, -681, 509, -795, -100, 391, -614, -115, 210, -836, -401, 413]
    [-987, -972, -945, -939, -846, -837, -836, -805, -802, -801, -795, -764, -763, -746, -709, -686, -681, -664, -660, -655, -615, -614, -614, -588, -588, -573, -573, -573, -562, -493, -492, -480, -461, -401, -386, -384, -373, -363, -295, -276, -254, -221, -210, -206, -134, -115, -100, -84, -49, -29, -17, 11, 28, 48, 88, 123, 124, 129, 150, 181, 181, 191, 192, 193, 199, 199, 203, 210, 229, 308, 311, 391, 399, 402, 413, 422, 433, 448, 495, 503, 509, 565, 599, 613, 667, 678, 680, 683, 796, 892, 897, 906, 916, 920, 927, 936, 938, 992, 997, 999]
    {192: 1, -254: 1, 796: 1, 920: 1, -764: 1, 28: 1, -972: 1, 48: 1, -709: 1, 613: 1, 927: 1, 191: 1, -480: 1, -461: 1, 199: 2, -686: 1, -846: 1, 448: 1, -49: 1, -588: 2, 129: 1, -295: 1, 229: 1, 433: 1, 938: 1, -363: 1, -573: 3, 181: 2, 565: 1, 906: 1, 667: 1, -615: 1, 422: 1, -614: 2, -492: 1, -373: 1, 678: 1, 123: 1, 193: 1, 308: 1, -84: 1, -493: 1, 503: 1, 203: 1, -837: 1, -386: 1, -210: 1, 897: 1, 683: 1, -206: 1, -987: 1, -660: 1, -801: 1, 402: 1, 311: 1, -562: 1, -655: 1, 936: 1, 997: 1, 892: 1, -134: 1, -664: 1, -805: 1, 124: 1, 399: 1, 992: 1, 999: 1, -802: 1, -17: 1, 11: 1, 599: 1, -221: 1, -746: 1, 680: 1, -276: 1, 88: 1, -945: 1, -29: 1, -939: 1, 495: 1, -763: 1, -384: 1, 916: 1, 150: 1, -681: 1, 509: 1, -795: 1, -100: 1, 391: 1, -115: 1, 210: 1, -836: 1, -401: 1, 413: 1}
    [(-987, 1), (-972, 1), (-945, 1), (-939, 1), (-846, 1), (-837, 1), (-836, 1), (-805, 1), (-802, 1), (-801, 1), (-795, 1), (-764, 1), (-763, 1), (-746, 1), (-709, 1), (-686, 1), (-681, 1), (-664, 1), (-660, 1), (-655, 1), (-615, 1), (-614, 2), (-588, 2), (-573, 3), (-562, 1), (-493, 1), (-492, 1), (-480, 1), (-461, 1), (-401, 1), (-386, 1), (-384, 1), (-373, 1), (-363, 1), (-295, 1), (-276, 1), (-254, 1), (-221, 1), (-210, 1), (-206, 1), (-134, 1), (-115, 1), (-100, 1), (-84, 1), (-49, 1), (-29, 1), (-17, 1), (11, 1), (28, 1), (48, 1), (88, 1), (123, 1), (124, 1), (129, 1), (150, 1), (181, 2), (191, 1), (192, 1), (193, 1), (199, 2), (203, 1), (210, 1), (229, 1), (308, 1), (311, 1), (391, 1), (399, 1), (402, 1), (413, 1), (422, 1), (433, 1), (448, 1), (495, 1), (503, 1), (509, 1), (565, 1), (599, 1), (613, 1), (667, 1), (678, 1), (680, 1), (683, 1), (796, 1), (892, 1), (897, 1), (906, 1), (916, 1), (920, 1), (927, 1), (936, 1), (938, 1), (992, 1), (997, 1), (999, 1)]
    需求二
    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    #EMAIL:y1053419035@qq.com
    
    
    import  random
    
    """
    字符串重复统计
        字符表'abcdefghijklmnopqrstuvwxyz'
        随机挑选2个字母组成字符串,共挑选100个
        降序输出所有不同的字符串及重复的次数
    """
    
    
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    
    words = []
    
    for _ in range(100):
        words.append("".join(random.choice(alphabet) for _ in range(2)))        #生成器
    
    d = {}
    
    for x in  words:
        d[x] = d.get(x,0) + 1
    
    print(d)
    
    d1 = sorted(d.items(),reverse=True)
    print(d1)
    
    
    
    #以上代码执行结果如下:
    {'jc': 2, 'sf': 1, 'ju': 1, 'ek': 2, 'il': 1, 'ii': 1, 'db': 1, 'wi': 1, 've': 1, 'ys': 1, 'hm': 1, 'qs': 1, 'hk': 1, 'ok': 1, 'kb': 1, 'uc': 1, 'zj': 1, 'go': 1, 'ot': 1, 'wm': 1, 'ca': 1, 'lz': 1, 'wc': 1, 'av': 1, 'li': 2, 'tn': 1, 'qh': 1, 'fw': 1, 'vr': 1, 'vg': 1, 'rb': 1, 'jf': 1, 'cy': 1, 'iv': 1, 'oi': 1, 'ms': 1, 'de': 1, 'hr': 1, 'ua': 1, 'lq': 1, 'yt': 1, 'nq': 1, 'hu': 1, 'uf': 2, 'cc': 1, 'qx': 1, 'kr': 2, 'ez': 1, 'em': 1, 'zh': 1, 'tb': 1, 'cg': 1, 'tz': 1, 'xf': 1, 'hb': 1, 'va': 1, 'lb': 3, 'nd': 1, 'we': 1, 'tw': 1, 'pj': 1, 'rf': 1, 'gg': 1, 'oc': 1, 'vs': 1, 'vk': 1, 'ap': 1, 'fx': 1, 'ut': 1, 'lr': 1, 'wb': 1, 'og': 1, 'pa': 1, 'cr': 1, 'ul': 1, 'oa': 1, 'yq': 2, 'sz': 1, 'fu': 1, 'wh': 1, 'si': 1, 'jb': 1, 'nx': 1, 'po': 1, 'fq': 1, 'lu': 1, 'uv': 1, 'vq': 1, 'py': 1, 'wg': 2, 'pc': 1}
    [('zj', 1), ('zh', 1), ('yt', 1), ('ys', 1), ('yq', 2), ('xf', 1), ('wm', 1), ('wi', 1), ('wh', 1), ('wg', 2), ('we', 1), ('wc', 1), ('wb', 1), ('vs', 1), ('vr', 1), ('vq', 1), ('vk', 1), ('vg', 1), ('ve', 1), ('va', 1), ('uv', 1), ('ut', 1), ('ul', 1), ('uf', 2), ('uc', 1), ('ua', 1), ('tz', 1), ('tw', 1), ('tn', 1), ('tb', 1), ('sz', 1), ('si', 1), ('sf', 1), ('rf', 1), ('rb', 1), ('qx', 1), ('qs', 1), ('qh', 1), ('py', 1), ('po', 1), ('pj', 1), ('pc', 1), ('pa', 1), ('ot', 1), ('ok', 1), ('oi', 1), ('og', 1), ('oc', 1), ('oa', 1), ('nx', 1), ('nq', 1), ('nd', 1), ('ms', 1), ('lz', 1), ('lu', 1), ('lr', 1), ('lq', 1), ('li', 2), ('lb', 3), ('kr', 2), ('kb', 1), ('ju', 1), ('jf', 1), ('jc', 2), ('jb', 1), ('iv', 1), ('il', 1), ('ii', 1), ('hu', 1), ('hr', 1), ('hm', 1), ('hk', 1), ('hb', 1), ('go', 1), ('gg', 1), ('fx', 1), ('fw', 1), ('fu', 1), ('fq', 1), ('ez', 1), ('em', 1), ('ek', 2), ('de', 1), ('db', 1), ('cy', 1), ('cr', 1), ('cg', 1), ('cc', 1), ('ca', 1), ('av', 1), ('ap', 1)]
    需求三
  • 相关阅读:
    Python os模块
    Python 常用模块
    CentOS7中配置基于Nginx+Supervisor+Gunicorn的Flask项目
    CentOS下安装Python3.4
    修改windows文件的换行符
    dubbo源码阅读-ProxyFactory(十一)之JdkProxyFactory
    dubbo源码阅读-Filter默认实现(十一)之DeprecatedFilter
    dubbo源码阅读-Filter默认实现(十一)之TimeoutFilter
    dubbo源码阅读-Filter默认实现(十一)之ExceptionFilter
    dubbo源码阅读-Filter默认实现(十一)之ContextFilter&ConsumerContextFilter
  • 原文地址:https://www.cnblogs.com/yinzhengjie/p/10897485.html
Copyright © 2011-2022 走看看