本文整理汇总了Python中collections.Counter.__init__方法的典型用法代码示例。如果您正苦于以下问题:Python Counter.__init__方法的具体用法?Python Counter.__init__怎么用?Python Counter.__init__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类collections.Counter
的用法示例。
在下文中一共展示了Counter.__init__方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_equality_Set
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import __init__ [as 别名]
def test_equality_Set(self):
class MySet(Set):
def __init__(self, itr):
self.contents = itr
def __contains__(self, x):
return x in self.contents
def __iter__(self):
return iter(self.contents)
def __len__(self):
return len([x for x in self.contents])
s1 = MySet((1,))
s2 = MySet((1, 2))
s3 = MySet((3, 4))
s4 = MySet((3, 4))
self.assertTrue(s2 > s1)
self.assertTrue(s1 < s2)
self.assertFalse(s2 <= s1)
self.assertFalse(s2 <= s3)
self.assertFalse(s1 >= s2)
self.assertEqual(s3, s4)
self.assertNotEqual(s2, s3)
示例2: test_init
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import __init__ [as 别名]
def test_init(self):
OrderedDict = self.OrderedDict
with self.assertRaises(TypeError):
OrderedDict([('a', 1), ('b', 2)], None) # too many args
pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
self.assertEqual(sorted(OrderedDict(dict(pairs)).items()), pairs) # dict input
self.assertEqual(sorted(OrderedDict(**dict(pairs)).items()), pairs) # kwds input
self.assertEqual(list(OrderedDict(pairs).items()), pairs) # pairs input
self.assertEqual(list(OrderedDict([('a', 1), ('b', 2), ('c', 9), ('d', 4)],
c=3, e=5).items()), pairs) # mixed input
# make sure no positional args conflict with possible kwdargs
self.assertEqual(list(OrderedDict(self=42).items()), [('self', 42)])
self.assertEqual(list(OrderedDict(other=42).items()), [('other', 42)])
self.assertRaises(TypeError, OrderedDict, 42)
self.assertRaises(TypeError, OrderedDict, (), ())
self.assertRaises(TypeError, OrderedDict.__init__)
# Make sure that direct calls to __init__ do not clear previous contents
d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)])
d.__init__([('e', 5), ('f', 6)], g=7, d=4)
self.assertEqual(list(d.items()),
[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])
示例3: __init__
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import __init__ [as 别名]
def __init__(self, samples=None):
"""
Construct a new frequency distribution. If ``samples`` is
given, then the frequency distribution will be initialized
with the count of each object in ``samples``; otherwise, it
will be initialized to be empty.
In particular, ``FreqDist()`` returns an empty frequency
distribution; and ``FreqDist(samples)`` first creates an empty
frequency distribution, and then calls ``update`` with the
list ``samples``.
:param samples: The samples to initialize the frequency
distribution with.
:type samples: Sequence
"""
Counter.__init__(self, samples)
# Cached number of samples in this FreqDist
self._N = None
示例4: validate_comparison
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import __init__ [as 别名]
def validate_comparison(self, instance):
ops = ['lt', 'gt', 'le', 'ge', 'ne', 'or', 'and', 'xor', 'sub']
operators = {}
for op in ops:
name = '__' + op + '__'
operators[name] = getattr(operator, name)
class Other:
def __init__(self):
self.right_side = False
def __eq__(self, other):
self.right_side = True
return True
__lt__ = __eq__
__gt__ = __eq__
__le__ = __eq__
__ge__ = __eq__
__ne__ = __eq__
__ror__ = __eq__
__rand__ = __eq__
__rxor__ = __eq__
__rsub__ = __eq__
for name, op in operators.items():
if not hasattr(instance, name):
continue
other = Other()
op(instance, other)
self.assertTrue(other.right_side,'Right side not called for %s.%s'
% (type(instance), name))
示例5: __init__
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import __init__ [as 别名]
def __init__(self, it=()):
self.data = set(it)
示例6: test_hash_Set
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import __init__ [as 别名]
def test_hash_Set(self):
class OneTwoThreeSet(Set):
def __init__(self):
self.contents = [1, 2, 3]
def __contains__(self, x):
return x in self.contents
def __len__(self):
return len(self.contents)
def __iter__(self):
return iter(self.contents)
def __hash__(self):
return self._hash()
a, b = OneTwoThreeSet(), OneTwoThreeSet()
self.assertTrue(hash(a) == hash(b))
示例7: test_issue_4920
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import __init__ [as 别名]
def test_issue_4920(self):
# MutableSet.pop() method did not work
class MySet(MutableSet):
__slots__=['__s']
def __init__(self,items=None):
if items is None:
items=[]
self.__s=set(items)
def __contains__(self,v):
return v in self.__s
def __iter__(self):
return iter(self.__s)
def __len__(self):
return len(self.__s)
def add(self,v):
result=v not in self.__s
self.__s.add(v)
return result
def discard(self,v):
result=v in self.__s
self.__s.discard(v)
return result
def __repr__(self):
return "MySet(%s)" % repr(list(self))
s = MySet([5,43,2,1])
self.assertEqual(s.pop(), 1)
示例8: test_init
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import __init__ [as 别名]
def test_init(self):
self.assertEqual(list(Counter(self=42).items()), [('self', 42)])
self.assertEqual(list(Counter(iterable=42).items()), [('iterable', 42)])
self.assertEqual(list(Counter(iterable=None).items()), [('iterable', None)])
self.assertRaises(TypeError, Counter, 42)
self.assertRaises(TypeError, Counter, (), ())
self.assertRaises(TypeError, Counter.__init__)
示例9: test_issue_4920
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import __init__ [as 别名]
def test_issue_4920(self):
# MutableSet.pop() method did not work
class MySet(collections.MutableSet):
__slots__=['__s']
def __init__(self,items=None):
if items is None:
items=[]
self.__s=set(items)
def __contains__(self,v):
return v in self.__s
def __iter__(self):
return iter(self.__s)
def __len__(self):
return len(self.__s)
def add(self,v):
result=v not in self.__s
self.__s.add(v)
return result
def discard(self,v):
result=v in self.__s
self.__s.discard(v)
return result
def __repr__(self):
return "MySet(%s)" % repr(list(self))
s = MySet([5,43,2,1])
self.assertEqual(s.pop(), 1)
示例10: test_override_update
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import __init__ [as 别名]
def test_override_update(self):
# Verify that subclasses can override update() without breaking __init__()
class MyOD(OrderedDict):
def update(self, *args, **kwds):
raise Exception()
items = [('a', 1), ('c', 3), ('b', 2)]
self.assertEqual(list(MyOD(items).items()), items)
示例11: test_isdisjoint_Set
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import __init__ [as 别名]
def test_isdisjoint_Set(self):
class MySet(Set):
def __init__(self, itr):
self.contents = itr
def __contains__(self, x):
return x in self.contents
def __iter__(self):
return iter(self.contents)
def __len__(self):
return len([x for x in self.contents])
s1 = MySet((1, 2, 3))
s2 = MySet((4, 5, 6))
s3 = MySet((1, 5, 6))
self.assertTrue(s1.isdisjoint(s2))
self.assertFalse(s1.isdisjoint(s3))
示例12: test_Sequence_mixins
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import __init__ [as 别名]
def test_Sequence_mixins(self):
class SequenceSubclass(Sequence):
def __init__(self, seq=()):
self.seq = seq
def __getitem__(self, index):
return self.seq[index]
def __len__(self):
return len(self.seq)
# Compare Sequence.index() behavior to (list|str).index() behavior
def assert_index_same(seq1, seq2, index_args):
try:
expected = seq1.index(*index_args)
except ValueError:
with self.assertRaises(ValueError):
seq2.index(*index_args)
else:
actual = seq2.index(*index_args)
self.assertEqual(
actual, expected, '%r.index%s' % (seq1, index_args))
for ty in list, str:
nativeseq = ty('abracadabra')
indexes = [-10000, -9999] + list(range(-3, len(nativeseq) + 3))
seqseq = SequenceSubclass(nativeseq)
for letter in set(nativeseq) | {'z'}:
assert_index_same(nativeseq, seqseq, (letter,))
for start in range(-3, len(nativeseq) + 3):
assert_index_same(nativeseq, seqseq, (letter, start))
for stop in range(-3, len(nativeseq) + 3):
assert_index_same(
nativeseq, seqseq, (letter, start, stop))