本文整理汇总了Python中bag.Bag.add方法的典型用法代码示例。如果您正苦于以下问题:Python Bag.add方法的具体用法?Python Bag.add怎么用?Python Bag.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bag.Bag
的用法示例。
在下文中一共展示了Bag.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_add
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import add [as 别名]
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')
示例2: test_add
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import add [as 别名]
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.")
示例3: test_add
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import add [as 别名]
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)
示例4: test_add
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import add [as 别名]
def test_add(self):
temp_list = [random.randint(1,10) for i in range(1,1001)]
b1 = Bag(temp_list)
b2 = Bag()
random.shuffle(temp_list)
for i in temp_list:
b2.add(i)
self.assertEqual(b1,b2)
示例5: test_add
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import add [as 别名]
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)
示例6: test_remove
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import add [as 别名]
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)
示例7: test_remove
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import add [as 别名]
def test_remove(self):
temp_list = []
for i in range(1,1001):
temp_list.append(random.randint(1,10))
b1 = Bag(temp_list)
self.assertRaises(ValueError,self.bag.remove,33)
b2 = Bag(temp_list)
for i in temp_list:
b2.add(i)
b2.remove(i)
self.assertEqual(b1,b2)
示例8: edges
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import add [as 别名]
def edges(self):
l = Bag()
for v in range(self._v):
self_loops = 0
for edge in self.adjacent(v):
if edge.other(v) > v:
l.add(edge)
elif edge.other(v) == v:
if self_loops % 2 == 0:
l.add(edge)
self_loops += 1
return l
示例9: test_remove
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import add [as 别名]
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.")
示例10: testAdd
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import add [as 别名]
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)
示例11: test_remove
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import add [as 别名]
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')
示例12: testRemove
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import add [as 别名]
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)
示例13: test_remove
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import add [as 别名]
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)
示例14: TestBag
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import add [as 别名]
class TestBag(unittest.TestCase):
def setUp(self):
self.bag = Bag()
def test_is_empty(self):
self.assertTrue(self.bag.is_empty())
self.bag.add(3)
self.assertTrue(not self.bag.is_empty())
def test_size(self):
self.assertEqual(self.bag.size(), 0)
self.bag.add(3)
self.assertEqual(self.bag.size(), 1)
def test_add(self):
self.bag.add(3)
self.assertEqual(self.bag._a, [3]) # This code smells.
def test_each(self):
self.bag.add(3)
self.bag.add('foo')
self.bag.add([])
lst = []
self.bag.each(lambda x: lst.append(x))
self.assertEqual([3, 'foo', []], lst)
示例15: Item
# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import add [as 别名]
from item import Item
from bag import Bag
from operator import attrgetter
items = [Item(150,20,1), Item(325,40,2), Item(600,50,3), Item(805,36,4), Item(430,25,5), Item(1200,64,6), Item(770,54,7), Item(60,18,8), Item(930,46,9), Item(353,28,10)]
b = Bag()
while True:
nxt = items.pop(items.index(max(items, key=attrgetter('_coef'))))
if b.getSize() + nxt.getVolume() <= 4200:
b.add(nxt)
else:
break
print(b)