zoukankan      html  css  js  c++  java
  • 默认字典defaultdict

    默认字典defaultdict
    defaultdict是对字典的类型的补充,他默认给字典的值设置了一个类型。
    class defaultdict(dict):
    """
    defaultdict(default_factory[, ...]) --> dict with default factory
    The default factory is called without arguments to produce
    a new value when a key is not present, in __getitem__ only.
    A defaultdict compares equal to a dict with the same items.
    All remaining arguments are treated the same as if they were
    passed to the dict constructor, including keyword arguments.
    """
    def copy(self): # real signature unknown; restored from __doc__
    """ D.copy() -> a shallow copy of D. """
    pass
    def __copy__(self, *args, **kwargs): # real signature unknown
    """ D.copy() -> a shallow copy of D. """
    pass
    def __getattribute__(self, name): # real signature unknown; restored from __doc__
    """ x.__getattribute__('name') <==> x.name """
    pass
    def __init__(self, default_factory=None, **kwargs): # known case of _collections.defaultdict.__init__
    """
    defaultdict(default_factory[, ...]) --> dict with default factory
    The default factory is called without arguments to produce
    a new value when a key is not present, in __getitem__ only.
    A defaultdict compares equal to a dict with the same items.
    All remaining arguments are treated the same as if they were
    passed to the dict constructor, including keyword arguments.
    # (copied from class doc)
    """
    pass
    def __missing__(self, key): # real signature unknown; restored from __doc__
    """
    __missing__(key) # Called by __getitem__ for missing key; pseudo-code:
    if self.default_factory is None: raise KeyError((key,))
    self[key] = value = self.default_factory()
    return value
    """
    pass
    def __reduce__(self, *args, **kwargs): # real signature unknown
    """ Return state information for pickling. """
    pass
    def __repr__(self): # real signature unknown; restored from __doc__
    """ x.__repr__() <==> repr(x) """
    pass
    default_factory = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
    """Factory for default value called by __missing__()."""
    原生字典:
    values = [11, 22, 33,44,55,66,77,88,99,90]
    my_dict = {}
    for value in values:
    if value>66:
    if my_dict.has_key('k1'):
    my_dict['k1'].append(value)
    else:
    my_dict['k1'] = [value]
    else:
    if my_dict.has_key('k2'):
    my_dict['k2'].append(value)
    else:
    my_dict['k2'] = [value]
    默认字典:
    from collections import defaultdict
    values = [11, 22, 33,44,55,66,77,88,99,90]
    my_dict = defaultdict(list)
    for value in values:
    if value>66:
    my_dict['k1'].append(value)
    else:
    my_dict['k2'].append(value)
    defaultdict字典解决方法
  • 相关阅读:
    程序员转型架构师,推荐你读这几本书
    Dubbo服务发现源码解析
    高可用架构之限流降级
    为什么Kafka速度那么快
    从分布式一致性到共识机制(三)拜占庭问题
    从分布式一致性到共识机制(二)Raft算法
    三分钟看完京东区块链白皮书
    轻松理解零知识证明
    三大去中心化交易协议对比
    从分布式一致性到共识机制(一)Paxos算法
  • 原文地址:https://www.cnblogs.com/skyzy/p/9433431.html
Copyright © 2011-2022 走看看