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


Python Counter.fromkeys方法代码示例

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


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

示例1: test_move_to_end

# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import fromkeys [as 别名]
def test_move_to_end(self):
        OrderedDict = self.OrderedDict
        od = OrderedDict.fromkeys('abcde')
        self.assertEqual(list(od), list('abcde'))
        od.move_to_end('c')
        self.assertEqual(list(od), list('abdec'))
        od.move_to_end('c', 0)
        self.assertEqual(list(od), list('cabde'))
        od.move_to_end('c', 0)
        self.assertEqual(list(od), list('cabde'))
        od.move_to_end('e')
        self.assertEqual(list(od), list('cabde'))
        od.move_to_end('b', last=False)
        self.assertEqual(list(od), list('bcade'))
        with self.assertRaises(KeyError):
            od.move_to_end('x')
        with self.assertRaises(KeyError):
            od.move_to_end('x', 0) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:20,代码来源:test_collections.py

示例2: test_key_change_during_iteration

# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import fromkeys [as 别名]
def test_key_change_during_iteration(self):
        OrderedDict = self.OrderedDict

        od = OrderedDict.fromkeys('abcde')
        self.assertEqual(list(od), list('abcde'))
        with self.assertRaises(RuntimeError):
            for i, k in enumerate(od):
                od.move_to_end(k)
                self.assertLess(i, 5)
        with self.assertRaises(RuntimeError):
            for k in od:
                od['f'] = None
        with self.assertRaises(RuntimeError):
            for k in od:
                del od['c']
        self.assertEqual(list(od), list('bdeaf')) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:18,代码来源:test_collections.py

示例3: fromkeys

# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import fromkeys [as 别名]
def fromkeys(cls, iterable, v=None):
                raise NotImplementedError(
                    'Counter.fromkeys() is undefined.  Use Counter(iterable) instead.') 
开发者ID:rafasashi,项目名称:razzy-spinner,代码行数:5,代码来源:compat.py

示例4: fromkeys

# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import fromkeys [as 别名]
def fromkeys(cls, iterable, value=None):
        '''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S.
        If not specified, the value defaults to None.

        '''
        self = cls()
        for key in iterable:
            self[key] = value
        return self 
开发者ID:Soft8Soft,项目名称:verge3d-blender-addon,代码行数:11,代码来源:misc.py

示例5: validate_abstract_methods

# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import fromkeys [as 别名]
def validate_abstract_methods(self, abc, *names):
        methodstubs = dict.fromkeys(names, lambda s, *args: 0)

        # everything should work will all required methods are present
        C = type('C', (abc,), methodstubs)
        C()

        # instantiation should fail if a required method is missing
        for name in names:
            stubs = methodstubs.copy()
            del stubs[name]
            C = type('C', (abc,), stubs)
            self.assertRaises(TypeError, C, name) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:15,代码来源:test_collections.py

示例6: test_multiset_operations

# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import fromkeys [as 别名]
def test_multiset_operations(self):
        # Verify that adding a zero counter will strip zeros and negatives
        c = Counter(a=10, b=-2, c=0) + Counter()
        self.assertEqual(dict(c), dict(a=10))

        elements = 'abcd'
        for i in range(1000):
            # test random pairs of multisets
            p = Counter(dict((elem, randrange(-2,4)) for elem in elements))
            p.update(e=1, f=-1, g=0)
            q = Counter(dict((elem, randrange(-2,4)) for elem in elements))
            q.update(h=1, i=-1, j=0)
            for counterop, numberop in [
                (Counter.__add__, lambda x, y: max(0, x+y)),
                (Counter.__sub__, lambda x, y: max(0, x-y)),
                (Counter.__or__, lambda x, y: max(0,x,y)),
                (Counter.__and__, lambda x, y: max(0, min(x,y))),
            ]:
                result = counterop(p, q)
                for x in elements:
                    self.assertEqual(numberop(p[x], q[x]), result[x],
                                     (counterop, x, p, q))
                # verify that results exclude non-positive counts
                self.assertTrue(x>0 for x in result.values())

        elements = 'abcdef'
        for i in range(100):
            # verify that random multisets with no repeats are exactly like sets
            p = Counter(dict((elem, randrange(0, 2)) for elem in elements))
            q = Counter(dict((elem, randrange(0, 2)) for elem in elements))
            for counterop, setop in [
                (Counter.__sub__, set.__sub__),
                (Counter.__or__, set.__or__),
                (Counter.__and__, set.__and__),
            ]:
                counter_result = counterop(p, q)
                set_result = setop(set(p.elements()), set(q.elements()))
                self.assertEqual(counter_result, dict.fromkeys(set_result, 1)) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:40,代码来源:test_collections.py

示例7: fromkeys

# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import fromkeys [as 别名]
def fromkeys(cls, iterable, value=None):
            d = cls()
            for key in iterable:
                d[key] = value
            return d 
开发者ID:muhanzhang,项目名称:D-VAE,代码行数:7,代码来源:python2x.py

示例8: test_repr_recursive

# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import fromkeys [as 别名]
def test_repr_recursive(self):
        # See issue #9826
        od = OrderedDict.fromkeys('abc')
        od['x'] = od
        self.assertEqual(repr(od),
            "OrderedDict([('a', None), ('b', None), ('c', None), ('x', ...)])") 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:8,代码来源:test_collections.py

示例9: test_views

# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import fromkeys [as 别名]
def test_views(self):
        s = 'the quick brown fox jumped over a lazy dog yesterday before dawn'.split()
        od = OrderedDict.fromkeys(s)
        self.assertEqual(list(od.viewkeys()),  s)
        self.assertEqual(list(od.viewvalues()),  [None for k in s])
        self.assertEqual(list(od.viewitems()),  [(k, None) for k in s]) 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:8,代码来源:test_collections.py

示例10: fromkeys

# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import fromkeys [as 别名]
def fromkeys(cls, iterable, value=None):
        """OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S and
        values equal to v (which defaults to None).
        """
        d = cls()
        for key in iterable:
            d[key] = value
        return d 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:10,代码来源:__init__.py

示例11: fromkeys

# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import fromkeys [as 别名]
def fromkeys(cls, iterable, value=None):
        '''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S and
        values equal to v (which defaults to None).
        '''
        d = cls()
        for key in iterable:
            d[key] = value
        return d 
开发者ID:hughperkins,项目名称:kgsgo-dataset-preprocessor,代码行数:10,代码来源:pandas_py3k.py


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