当前位置: 首页>>代码示例>>Python>>正文


Python MutableMapping.items方法代码示例

本文整理汇总了Python中collections.MutableMapping.items方法的典型用法代码示例。如果您正苦于以下问题:Python MutableMapping.items方法的具体用法?Python MutableMapping.items怎么用?Python MutableMapping.items使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在collections.MutableMapping的用法示例。


在下文中一共展示了MutableMapping.items方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: elements

# 需要导入模块: from collections import MutableMapping [as 别名]
# 或者: from collections.MutableMapping import items [as 别名]
def elements(self):
        '''Iterator over elements repeating each as many times as its count.

        >>> c = Counter('ABCABC')
        >>> sorted(c.elements())
        ['A', 'A', 'B', 'B', 'C', 'C']

        # Knuth's example for prime factors of 1836:  2**2 * 3**3 * 17**1
        >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
        >>> product = 1
        >>> for factor in prime_factors.elements():     # loop over factors
        ...     product *= factor                       # and multiply them
        >>> product
        1836

        Note, if an element's count has been set to zero or is a negative
        number, elements() will ignore it.

        '''
        # Emulate Bag.do from Smalltalk and Multiset.begin from C++.
        return _chain.from_iterable(_starmap(_repeat, self.items()))

    # Override dict methods where necessary 
开发者ID:Soft8Soft,项目名称:verge3d-blender-addon,代码行数:25,代码来源:misc.py

示例2: __repr__

# 需要导入模块: from collections import MutableMapping [as 别名]
# 或者: from collections.MutableMapping import items [as 别名]
def __repr__(self):
        if not self:
            return '%s()' % self.__class__.__name__
        try:
            items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
            return '%s({%s})' % (self.__class__.__name__, items)
        except TypeError:
            # handle case where values are not orderable
            return '{0}({1!r})'.format(self.__class__.__name__, dict(self))

    # Multiset-style mathematical operations discussed in:
    #       Knuth TAOCP Volume II section 4.6.3 exercise 19
    #       and at http://en.wikipedia.org/wiki/Multiset
    #
    # Outputs guaranteed to only include positive counts.
    #
    # To strip negative and zero counts, add-in an empty counter:
    #       c += Counter() 
开发者ID:Soft8Soft,项目名称:verge3d-blender-addon,代码行数:20,代码来源:misc.py

示例3: __add__

# 需要导入模块: from collections import MutableMapping [as 别名]
# 或者: from collections.MutableMapping import items [as 别名]
def __add__(self, other):
        '''Add counts from two counters.

        >>> Counter('abbb') + Counter('bcc')
        Counter({'b': 4, 'c': 2, 'a': 1})

        '''
        if not isinstance(other, Counter):
            return NotImplemented
        result = Counter()
        for elem, count in self.items():
            newcount = count + other[elem]
            if newcount > 0:
                result[elem] = newcount
        for elem, count in other.items():
            if elem not in self and count > 0:
                result[elem] = count
        return result 
开发者ID:Soft8Soft,项目名称:verge3d-blender-addon,代码行数:20,代码来源:misc.py

示例4: __sub__

# 需要导入模块: from collections import MutableMapping [as 别名]
# 或者: from collections.MutableMapping import items [as 别名]
def __sub__(self, other):
        ''' Subtract count, but keep only results with positive counts.

        >>> Counter('abbbc') - Counter('bccd')
        Counter({'b': 2, 'a': 1})

        '''
        if not isinstance(other, Counter):
            return NotImplemented
        result = Counter()
        for elem, count in self.items():
            newcount = count - other[elem]
            if newcount > 0:
                result[elem] = newcount
        for elem, count in other.items():
            if elem not in self and count < 0:
                result[elem] = 0 - count
        return result 
开发者ID:Soft8Soft,项目名称:verge3d-blender-addon,代码行数:20,代码来源:misc.py

示例5: __or__

# 需要导入模块: from collections import MutableMapping [as 别名]
# 或者: from collections.MutableMapping import items [as 别名]
def __or__(self, other):
        '''Union is the maximum of value in either of the input counters.

        >>> Counter('abbb') | Counter('bcc')
        Counter({'b': 3, 'c': 2, 'a': 1})

        '''
        if not isinstance(other, Counter):
            return NotImplemented
        result = Counter()
        for elem, count in self.items():
            other_count = other[elem]
            newcount = other_count if count < other_count else count
            if newcount > 0:
                result[elem] = newcount
        for elem, count in other.items():
            if elem not in self and count > 0:
                result[elem] = count
        return result 
开发者ID:Soft8Soft,项目名称:verge3d-blender-addon,代码行数:21,代码来源:misc.py

示例6: __and__

# 需要导入模块: from collections import MutableMapping [as 别名]
# 或者: from collections.MutableMapping import items [as 别名]
def __and__(self, other):
        ''' Intersection is the minimum of corresponding counts.

        >>> Counter('abbb') & Counter('bcc')
        Counter({'b': 1})

        '''
        if not isinstance(other, Counter):
            return NotImplemented
        result = Counter()
        for elem, count in self.items():
            other_count = other[elem]
            newcount = count if count < other_count else other_count
            if newcount > 0:
                result[elem] = newcount
        return result 
开发者ID:remg427,项目名称:misp42splunk,代码行数:18,代码来源:misc.py

示例7: clear

# 需要导入模块: from collections import MutableMapping [as 别名]
# 或者: from collections.MutableMapping import items [as 别名]
def clear(self):
        'od.clear() -> None.  Remove all items from od.'
        root = self.__root
        root.prev = root.next = root
        self.__map.clear()
        dict.clear(self) 
开发者ID:Soft8Soft,项目名称:verge3d-blender-addon,代码行数:8,代码来源:misc.py

示例8: __reduce__

# 需要导入模块: from collections import MutableMapping [as 别名]
# 或者: from collections.MutableMapping import items [as 别名]
def __reduce__(self):
        'Return state information for pickling'
        inst_dict = vars(self).copy()
        for k in vars(OrderedDict()):
            inst_dict.pop(k, None)
        return self.__class__, (), inst_dict or None, None, iter(self.items()) 
开发者ID:Soft8Soft,项目名称:verge3d-blender-addon,代码行数:8,代码来源:misc.py

示例9: most_common

# 需要导入模块: from collections import MutableMapping [as 别名]
# 或者: from collections.MutableMapping import items [as 别名]
def most_common(self, n=None):
        '''List the n most common elements and their counts from the most
        common to the least.  If n is None, then list all element counts.

        >>> Counter('abcdeabcdabcaba').most_common(3)
        [('a', 5), ('b', 4), ('c', 3)]

        '''
        # Emulate Bag.sortedByCount from Smalltalk
        if n is None:
            return sorted(self.items(), key=_itemgetter(1), reverse=True)
        return _heapq.nlargest(n, self.items(), key=_itemgetter(1)) 
开发者ID:Soft8Soft,项目名称:verge3d-blender-addon,代码行数:14,代码来源:misc.py

示例10: subtract

# 需要导入模块: from collections import MutableMapping [as 别名]
# 或者: from collections.MutableMapping import items [as 别名]
def subtract(*args, **kwds):
        '''Like dict.update() but subtracts counts instead of replacing them.
        Counts can be reduced below zero.  Both the inputs and outputs are
        allowed to contain zero and negative counts.

        Source can be an iterable, a dictionary, or another Counter instance.

        >>> c = Counter('which')
        >>> c.subtract('witch')             # subtract elements from another iterable
        >>> c.subtract(Counter('watch'))    # subtract elements from another counter
        >>> c['h']                          # 2 in which, minus 1 in witch, minus 1 in watch
        0
        >>> c['w']                          # 1 in which, minus 1 in witch, minus 1 in watch
        -1

        '''
        if not args:
            raise TypeError("descriptor 'subtract' of 'Counter' object "
                            "needs an argument")
        self = args[0]
        args = args[1:]
        if len(args) > 1:
            raise TypeError('expected at most 1 arguments, got %d' % len(args))
        iterable = args[0] if args else None
        if iterable is not None:
            self_get = self.get
            if isinstance(iterable, Mapping):
                for elem, count in iterable.items():
                    self[elem] = self_get(elem, 0) - count
            else:
                for elem in iterable:
                    self[elem] = self_get(elem, 0) - 1
        if kwds:
            self.subtract(kwds) 
开发者ID:Soft8Soft,项目名称:verge3d-blender-addon,代码行数:36,代码来源:misc.py


注:本文中的collections.MutableMapping.items方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。