Reputation: 83
class Bag:
def __init__(self, i=None):
self.bag = []
if i == None:
pass # i is None, do nothing after create empty bag
elif type(i)==list:
self.bag.extend(i) # append whole list i into bag with extend method
else:
self.bag.append(i) # append single item i into bag
def __repr__(self):
for s in self.bag :
return s
def __str__(self):
for s in self.bag :
return s
In the __str__method. It supposed to return a string.
The list is Bag(['a', 'c', 'b', 'b', 'd', 'd', 'd']). And
__str__ is supposed to return Bag(a[1], c[1], b[2], d[3])
Can anyone tell me how to make it work? Many thanks.
Upvotes: 0
Views: 37
Reputation: 309929
You can use a collections.Counter
here to help:
def __str__(self):
counts = Counter(self.bag)
count_str = ','.join('{}[{}]'.format(k, v) for k, v in counts.items())
return 'Bag({})'.format(count_str)
The great thing about this is that the counter does all the work of figuring out how many of each item are in your bag
. All the rest of it is just formatting details. One downside is that the counter is not ordered. If you want an order, you could use the .most_common
method (which will order the counts from most common to least-common), or you could use something like an ordered unique recipe to figure out how you want to iterate over the counter.
Upvotes: 1