zoukankan      html  css  js  c++  java
  • python成长之路第二篇(4)_collections系列

    一、分别取出大于66的数字和小于66的数字

    小练习:需求要求有一个列表列表中存着一组数字,要求将大于66的数字和小于66的数字分别取出来

    aa = [11,22,33,44,55,66,77,88,99,90]
    dic = {}
    for i in aa :
        ifi <= 66 :
            if 'k1' in dic.keys():
                dic['k1'].append(i)
            else:
                #创建只有一项元素的列表
              
    dic['k1'] = [i,]   #为了规范请使用逗号
        else:
            if 'k2' in dic.keys():
                dic['k2'].append(i)
            else:
                dic['k2'] = [i,]

    print(dic['k1'])
    print(dic['k2'])
    #k1和k2就分别保存了大于66和小于66的结果

     

    二、collections系列

    (1)Counter计数器

    Counter是对字典类型的补充,用于追踪值的出现次数。

    ps:具备字典的所有功能 + 自己的功能

    例子:
    import collections
    f1 = collections.Counter('asdwqewqdsadsadsa')
    print(f1)
    结果:
    Counter({'a': 4, 'd': 4, 's': 4, 'q': 2, 'w': 2, 'e': 1})
    #这个例子就是统计出元素出现的次数

    2)有序字典class OrderedDict(dict):

    orderdDict是对字典类型的补充,他记住了字典元素添加的顺序,从而使字典变得有序

    例子:

    import collections
    f1 = collections.OrderedDict()
    f1["a"] = 3
    f1["c"] = 2
    f1["b"] = 1
    f2 = {}
    f2["a"] = 3
    f2["c"] = 2
    f2["b"] = 1
    print(f1,f2)

    结果:OrderedDict([('a', 3), ('c', 2), ('b', 1)]) {'b': 1, 'c': 2, 'a': 3}

    从上面就可以发现,想同值的字典使用OrderedDict就按照添加顺序进行排序

    内部机制:

    在正常的字典中f2这么排序的:

    f2 = {'b': 1, 'c': 2, 'a': 3}

    而使用OrderedDict后他会生成一个列表来保存字典添加的顺序

    f1 = {'b': 1, 'c': 2, 'a': 3}

    f1_list = ["a","c","b"]

    这样就可以通过for循环来得到有序的字典

    不要较真哈


    (3)默认字典(defaultdict)

    defaultdict是对字典的类型的补充,他默认给字典的值设置了一个类型。

    import collections
    f1 = collections.defaultdict(list)
    f1["a"].append(1)
    print(f1)
    上面的和下面的效果相等,所以defaultidict是把value值设置成默认为list类型也就是列表类型
    f1 = {}
    f1["a"] = []
    f1["a"].append(1)
    (4)可命名元组namedtuple

    也就是将值提前起个名字,然后传值,主要用于平面图想x,y轴

    import collections

    atuple = collections.namedtuple("atuple",['x','y','z'])
    new = atuple(1,2,3,)
    print(new)
    atuple(x=1, y=2, z=3)
    (5)双向队列collections.deque

    双向队列为,例如有一个列表,这个列表呢 可以从左面和右面取数据,也可以从左面和右面同时加数据

    方法:

    (1)def append(self, *args, **kwargs): 添加右侧
          # real signature unknown
               """ Add an element to the right side of the dequ
    ‘. """
               
    pass
    (2)def appendleft(self, *args, **kwargs): 添加到队列的左侧
               # real signature unknown
                """ Add an element to the left side of the deque. """
                
    pass
    (3)clear(self, *args, **kwargs): 清除
      real signature unknown
       "" Remove all elements from the deque. """
       
    ass

    (4)f count(self, value): 元素出现的的个数
       real signature unknown; restored from __doc__
         """ D.count(value) -> integer -- return number of occurrences of value """
        
    return 0
    (5)def extend(self, *args, **kwargs):扩展右添加
       # real signature unknown
         """ Extend the right side of the deque with elements from the iterable
    """
        
    pass
    (6)def extendleft(self, *args, **kwargs): 扩展左添加
        # real signature unknown
         """ Extend the left side of the deque with elements from the iterable """
        
    pass
    (7)def pop(self, *args, **kwargs): 从右侧取出元素
       # real signature unknown
         """ Remove and return the rightmost element. """
        
    pass
    (8)def popleft(self, *args, **kwargs):从左侧取出元素
        real signature unknown
         """ Remove and return the leftmost element. """
        
    pass
    (9)def remove(self, value): 删除指定元素
       # real signature unknown; restored from __doc__
         """ D.remove(value) -- remove first occurrence of value. """
        
    pass
    (10)def reverse(self): 翻转
        # real signature unknown; restored from __doc__
         """ D.reverse() -- reverse *IN PLACE* """
        
    pass

    三、单向队列Queue

        q =Queue.Queue(10) 创建单向队列,括号中为队列中可放入元素的个数
        q.put 添加元素
        q.get 取出元素
    • 队列的特点:谁先进入谁先出去
    • 栈的特点:弹夹结构,后加的先出
  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    2.5.3 表单过滤器
    2.5.2 子过滤器
    2.5.1 位置过滤器:
    2.4 通过属性来选择元素
    2.3 根据层级查找元素
    2.2.4 元素选择器
    2.2.3 Class选择器
    2.2.2 ID选择器
  • 原文地址:https://www.cnblogs.com/bj-xy/p/5207301.html
Copyright © 2011-2022 走看看