當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。