本文整理汇总了Python中bag.Bag.count方法的典型用法代码示例。如果您正苦于以下问题:Python Bag.count方法的具体用法?Python Bag.count怎么用?Python Bag.count使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bag.Bag
的用法示例。
在下文中一共展示了Bag.count方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Test_Bag
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import count [as 别名]
class Test_Bag(unittest.TestCase):
def setUp(self):
self.original = ['d', 'a', 'b', 'd', 'c', 'b', 'd']
self.bag = Bag(['d', 'a', 'b', 'd', 'c', 'b', 'd'])
def test_len(self):
self.assertEqual(len(self.bag), 7, msg = "Length of constructed bag is not 7.")
my_list = self.original.copy()
for i in range(6, -1, -1):
s = random.choice(my_list)
self.bag.remove(s)
my_list.remove(s)
self.assertEqual(len(self.bag), i, msg = "Length of constructed bag is not " + str(i) +".")
self.assertEqual(len(self.bag), 0, msg = "Final length of constructed bag is not 0.")
def test_unique(self):
comp_set = set(self.original)
self.assertIs(self.bag.unique(), 4, msg = "Bag is not unique.")
my_list = self.original.copy()
while len(comp_set) != 0:
s = random.choice(my_list)
self.bag.remove(s)
my_list.remove(s)
comp_set = set(my_list)
self.assertIs(self.bag.unique(), len(comp_set), msg = "Bag is not unique.")
def test_contains(self):
comp_set = ['a', 'b', 'c', 'd']
while len(comp_set) != 0:
self.assertTrue(Bag.__contains__(self.bag, comp_set[0]), "'{}' not in bag.".format(str(comp_set[0])))
comp_set.remove(comp_set[0])
self.assertFalse(Bag.__contains__(self.bag, 'x'), "'x' is in bag.")
def test_count(self):
self.assertEqual(self.bag.count('a'), 1, msg = "count('a') should be 1")
self.assertEqual(self.bag.count('b'), 2, msg = "count('b') should be 2")
self.assertEqual(self.bag.count('c'), 1, msg = "count('c') should be 1")
self.assertEqual(self.bag.count('d'), 3, msg = "count('d') should be 3")
self.assertEqual(self.bag.count('x'), 0, msg = "count('x') should be 0")
total_count = self.bag.count('a') + self.bag.count('b') + self.bag.count('c') + self.bag.count('d')
my_list = self.original.copy()
while total_count != 0:
total_count -= 1
s = random.choice(my_list)
self.bag.remove(s)
my_list.remove(s)
self.assertEqual(self.bag.count('a') + self.bag.count('b') + self.bag.count('c') + self.bag.count('d'), total_count, "Sum of counts not equal " + str(total_count))
self.assertEqual(self.bag.count('a') + self.bag.count('b') + self.bag.count('c') + self.bag.count('d'), total_count, "Final sum of counts not equal to 0.")
def test_equal(self):
to_bag = [random.randrange(1, 11) for i in range(1000)]
bag1 = Bag(to_bag)
bag2 = Bag(to_bag)
self.assertEqual(bag1, bag2, "Bags are not equal.")
bag1.remove(to_bag[0])
self.assertNotEqual(bag1, bag2, "Bags should not be equal.")
def test_add(self):
to_bag = [random.randrange(1, 11) for i in range(1000)]
bag1 = Bag(to_bag)
random.shuffle(to_bag)
bag2 = Bag()
for i in to_bag:
bag2.add(i)
self.assertEqual(bag1, bag2, "Bags are not equal.")
def test_remove(self):
to_bag = [random.randrange(1, 11) for i in range(1000)]
bag1 = Bag(to_bag)
self.assertRaises(ValueError, Bag.remove, bag1, 32)
bag2 = Bag(to_bag)
for i in to_bag:
bag2.add(i)
for i in to_bag:
bag2.remove(i)
self.assertEqual(bag1, bag2, "Bags are not equal.")
示例2: Test_Bag
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import count [as 别名]
class Test_Bag(unittest.TestCase):
def setUp(self):
self.alist = ['d','a','b','d','c','b','d']
self.bag = Bag(self.alist)
def test_len(self):
self.assertEqual(len(self.bag),7)
size = 7
random.shuffle(self.alist)
for i in self.alist:
self.bag.remove(i)
size -= 1
self.assertEqual(len(self.bag),size)
self.assertEqual(len(self.bag),0)
def test_unique(self):
self.assertEqual(self.bag.unique(),4)
random.shuffle(self.alist)
for i in self.alist:
self.bag.remove(i)
self.assertEqual(self.bag.unique(),len(set(self.bag)))
def test_contains(self):
for v in ['a','b','c','d']:
self.assertIn(v,self.bag)
self.assertNotIn('x',self.bag)
def test_count(self):
for v,c in zip(('a','b','c','d'), (1,2, 1, 3)):
self.assertEqual(self.bag.count(v),c)
random.shuffle(self.alist)
size = 7
for i in self.alist:
self.bag.remove(i)
size -= 1
sum_count = sum([self.bag.count(c) for c in ['a','b','c','d']])
self.assertEqual(sum_count,size)
self.assertEqual(len(self.bag),0)
def test_equals(self):
alist = [random.randint(1,10) for i in range(1000)]
b1 = Bag(alist)
random.shuffle(alist)
b2 = Bag(alist)
self.assertEqual(b1,b2)
b1.remove(alist[0])
self.assertNotEquals(b1,b2)
def test_add(self):
alist = [random.randint(1,10) for i in range(1000)]
b1 = Bag(alist)
random.shuffle(alist)
b2 = Bag()
for v in alist:
b2.add(v)
self.assertEqual(b1,b2)
def test_remove(self):
alist = [random.randint(1,10) for i in range(1000)]
b1 = Bag(alist)
self.assertRaises(ValueError,b1.remove,11)
b2 = Bag(alist)
random.shuffle(alist)
for v in alist:
b2.add(v)
for v in alist:
b2.remove(v)
self.assertEqual(b1,b2)
示例3: Test_Bag
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import count [as 别名]
class Test_Bag(unittest.TestCase):
def setUp(self):
self.alist = ['d','a','b','d','c','b','d']
self.bag = Bag(self.alist)
def test_len(self):
print('Checking for length:',self.alist)
self.assertEqual(len(self.bag),7, 'Initial length of the self.bag is not 7')
bag_length = 7
for l in self.alist:
self.bag.remove(l)
bag_length -= 1
self.assertEqual(len(self.bag),bag_length, 'length of bag object is {lenbag} but it is supposed to be {good}'.format(lenbag = str(len(self.bag)), good = str(bag_length)))
def test_unique(self):
print('Checking for length:',self.alist)
self.assertEqual(self.bag.unique(), 4, 'Intial unique values in self.bag is not 4')
alist = self.alist
for i in range(len(self.alist)):
self.bag.remove(self.alist[i])
alist = self.alist[i+1:]
self.assertEqual(self.bag.unique(),len(set(alist)), 'Number of unique value in bag is {uniqueb} but should be {setlength}'.format(uniqueb = str(self.bag.unique()) ,setlength = str(len(set(alist)))))
def test_contains(self):
print('Checking for contains:',self.alist)
for el in ['a', 'b', 'c','d']:
self.assertTrue(el in self.bag, '{el} is not in self.bag')
self.assertFalse('x' in self.bag, 'x is in self.bag')
def test_count(self):
print('Checking for counts:',self.alist)
for num, el in [(1,'a'), (2, 'b'), (1, 'c'), (3, 'd'), (0, 'x')]:
self.assertEqual(num, self.bag.count(el), 'Initially self.bag contains {num}{el} but it contains {numb}'.format(num = str(num), el = str(el), numb = str(self.bag.count(el))))
init_sum = 7
for l in self.alist:
self.bag.remove(l)
init_sum -= 1
bag_sum = sum([self.bag.count(i) for i in ['a','b','c','d']])
self.assertEqual(init_sum, bag_sum , 'sum of the counts of abcd should {init_sum} but it is {bag_sum}'.format(init_sum = init_sum, bag_sum = bag_sum))
def test_equal(self):
print('Checking for equal')
alist = [random.randint(1,10) for i in range(1000)]
bag1 = Bag(alist)
random.shuffle(alist)
bag2 = Bag(alist)
self.assertEqual(bag1, bag2, 'Two back must be equal initially')
bag2.remove(alist[0])
self.assertNotEqual(bag1, bag2, 'Two back must not be equal after removing the first element of bag2')
def test_add(self):
print('Checking for add')
bag1 = Bag([random.randint(1,10) for i in range(1000)])
bag2 = Bag()
for el in iter(bag1):
bag2.add(el)
self.assertEqual(bag1,bag2, 'bag1 and bag 2 must be equal after adding all terms')
def test_remove(self):
print('Checking for remove')
alist = [random.randint(1,10) for i in range(1000)]
bag1 = Bag(alist)
self.assertRaises(ValueError, Bag.remove,bag1,32)
bag2 = Bag(alist)
for el in alist:
bag2.add(el)
for el in alist:
bag2.remove(el)
self.assertEqual(bag1,bag2, 'Two bag must be same after removing elements from bag2')
示例4: Test_Bag
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import count [as 别名]
class Test_Bag(unittest.TestCase):
def setUp(self):
self.alist = ['d','a','b','d','c','b','d']
self.bag = Bag(self.alist)
def test_len(self):
self.assertEqual(len(self.bag), 7)
for i in range(len(self.bag)):
self.bag.remove(self.alist[0])
self.alist.pop(0)
random.shuffle(self.alist)
self.assertEqual(len(self.bag), 7-(i+1))
def test_unique(self):
self.assertEqual(self.bag.unique(), 4)
for i in range (len(self.bag)):
self.bag.remove(self.alist[0])
self.alist.pop(0)
random.shuffle(self.alist)
self.assertEqual(self.bag.unique(), len(set(self.alist)))
def test_contains(self):
self.assertIn('a', self.bag)
self.assertIn('b', self.bag)
self.assertIn('c', self.bag)
self.assertIn('d', self.bag)
self.assertNotIn('x', self.bag)
def test_count(self):
self.assertEqual(self.bag.count('a'),1)
self.assertEqual(self.bag.count('b'),2)
self.assertEqual(self.bag.count('c'),1)
self.assertEqual(self.bag.count('d'),3)
self.assertEqual(self.bag.count('x'),0)
for i in range(len(self.bag)):
self.bag.remove(self.alist[0])
self.alist.pop(0)
random.shuffle(self.alist)
sum_num = 0
for v in self.bag.counts.values():
sum_num+=v
self.assertEqual(sum_num, 7-(i+1))
def test_eq(self):
bag2 = []
for i in range(0,1000):
bag2.append((random.randint(1,10)))
check_bag = Bag(bag2)
random.shuffle(bag2)
check_bag2 = Bag(bag2)
self.assertEqual(check_bag,check_bag2)
check_bag.remove(bag2[0])
self.assertNotEqual(check_bag,check_bag2)
def test_add(self):
bag2 = []
for i in range(0,1000):
bag2.append((random.randint(1,10)))
check_bag = Bag(bag2)
check_bag2 = Bag()
random.shuffle(bag2)
for i in check_bag:
check_bag2.add(i)
self.assertEqual(check_bag,check_bag2)
def test_remove(self):
bag2 = []
for i in range(0,1000):
bag2.append((random.randint(1,10)))
check_bag = Bag(bag2)
self.assertRaises(ValueError, check_bag.remove,21)
check_bag2 = Bag(bag2)
for i in bag2:
check_bag2.add(i)
for i in bag2:
check_bag2.remove(i)
self.assertEqual(check_bag,check_bag2)
示例5: Test_Bag
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import count [as 别名]
class Test_Bag(unittest.TestCase):
def setUp(self):
self.test_list = ['d','a','b','d','c','b','d']
self.bag = Bag(['d','a','b','d','c','b','d'])
def testLen(self):
self.assertEqual(len(self.bag), 7)
removal = ['d','a','b','d','c','b','d']
random.shuffle(removal)
for r in range(len(removal)):
self.bag.remove(removal[r])
self.assertEqual(len(self.bag), 7-(r+1))
def testUnique(self):
self.setUp()
self.assertEqual(self.bag.unique(),4)
for i in self.test_list:
self.bag.remove(i)
self.test_list.remove(i)
self.assertEqual(self.bag.unique(),len(set(self.test_list)))
def testContains(self):
self.setUp()
self.assertTrue('a' in self.bag)
self.assertTrue('b' in self.bag)
self.assertTrue('c' in self.bag)
self.assertTrue('d' in self.bag)
self.assertFalse('x' in self.bag)
def testCount(self):
self.setUp()
self.assertEqual(self.bag.count('a'),1)
self.assertEqual(self.bag.count('b'),2)
self.assertEqual(self.bag.count('c'),1)
self.assertEqual(self.bag.count('d'),3)
self.assertEqual(self.bag.count('x'),0)
random.shuffle(self.test_list)
for i in range(len(self.test_list)):
self.bag.remove(self.test_list[i])
sum_of_count = self.bag.count('a')+self.bag.count('b')+self.bag.count('c')+self.bag.count('d')
self.assertEqual(sum_of_count, 7-(i+1))
def testEqual(self):
test_list = [random.randint(1,10) for i in range(1000)]
test_bag1 = Bag(test_list)
random.shuffle(test_list)
test_bag2 = Bag(test_list)
self.assertTrue(test_bag1==test_bag2)
test_bag2.remove(test_list[0])
self.assertFalse(test_bag1==test_bag2)
def testAdd(self):
test_list = [random.randint(1,10) for i in range(1000)]
test_bag1 = Bag(test_list)
test_bag2 = Bag()
random.shuffle(test_list)
for i in test_list:
test_bag2.add(i)
self.assertTrue(test_bag1==test_bag2)
def testRemove(self):
test_list = [random.randint(1,10) for i in range(1000)]
test_bag1 = Bag(test_list)
self.assertRaises(ValueError,test_bag1.remove, 21)
test_bag2 = Bag(test_list)
for i in test_list:
test_bag2.add(i)
for i in test_list:
test_bag2.remove(i)
self.assertEqual(test_bag1,test_bag2)