zoukankan      html  css  js  c++  java
  • python 内置模块--collections

    1.计数器(counter)
     Counter是对字典的补充,用于追踪值出现的次数。
     Counter具有字典的全部属性和自己的属性。
    
    
    >>>import collections
    obj = collections.Counter('asasasasfageegadgsdga')
    print(obj)
    ret = obj.most_common(3)  #取出计数列的前3项
    print(ret)
    for i in obj.elements(): #elements用来取出计数器中的所有元素。
    print(i)
    for k,v in obj.items():  #用items取出计数器中的keys和values
    print(k,v)
    >>>obj = collections.Counter(['11', '22', '33', '22', '11', '22'])
    print(obj)
    obj.update(['11', '11', 'alex']) #更新数据
    print(obj)
    obj.subtract(['11'])  #从计数器中减去相应的元素
    obj.__delitem__(['11'])  #删除一个元素
     
    2.有序字典:
    dic = collections.OrderedDict()
    dic['K1'] = 'V1'
    dic['K2'] = 'V2'
    dic['K3'] = 'V3'
    dic['K4'] = 'V4'
    dic.update({'K1':'V232', 'K5':'V5'}) #数据存在的话就更新,不存在的话就添加
    dic.clear()    #清空
    dic.copy()     #复制
    
    
    
    
    class OrderedDict(dict):
    """ Dictionary that remembers insertion order """
    def clear(self): # real signature unknown; restored from __doc__
    """ od.clear() -> None. Remove all items from od. """
    pass

    def copy(self): # real signature unknown; restored from __doc__
    """ od.copy() -> a shallow copy of od """
    pass

    def fromkeys(cls, S, v=None): # real signature unknown; restored from __doc__
    """
    OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S.
    If not specified, the value defaults to None.
    """
    pass

    def items(self, *args, **kwargs): # real signature unknown
    pass

    def keys(self, *args, **kwargs): # real signature unknown
    pass

    def move_to_end(self, *args, **kwargs): # real signature unknown
    """
    Move an existing element to the end (or beginning if last==False).

    Raises KeyError if the element does not exist.
    When last=True, acts like a fast version of self[key]=self.pop(key).
    """
    pass

    def pop(self, k, d=None): # real signature unknown; restored from __doc__
    """
    od.pop(k[,d]) -> v, remove specified key and return the corresponding
    value. If key is not found, d is returned if given, otherwise KeyError
    is raised.
    """
    pass

    def popitem(self): # real signature unknown; restored from __doc__
    """
    od.popitem() -> (k, v), return and remove a (key, value) pair.
    Pairs are returned in LIFO order if last is true or FIFO order if false.
    """
    pass

    def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
    """ od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od """
    pass

    def update(self, *args, **kwargs): # real signature unknown
    pass

    def values(self, *args, **kwargs): # real signature unknown
    pass
    3.默认字典(defaultdict):
    dic = collections.defaultdict(list)
    在默认字典中,可以指定字典的values值的类型
    eg:
    import collections
    dic = collections.defaultdict(list)
    num = [11, 22, 33, 44, 55, 66, 77, 7, 88, 99]
    for i in num:
    if i>44:
    dic['k1'].append(i)
    else:
    dic['k2'].append(i)
    print(dic)
  • 相关阅读:
    正则表达式--验证中国手机号
    PostgreSQ数据库安全连接请求问题
    golang时间正反格式化
    Git 分支管理和冲突解决
    golang交叉编译
    ps命令
    Redis应用场景
    SecureCRT for Mac
    Redis作者谈Redis应用场景
    redis 五种数据的应用场景
  • 原文地址:https://www.cnblogs.com/ernest-zhang/p/5378483.html
Copyright © 2011-2022 走看看