Python: Probablity Calculator | Unable to restore original list after a draw

import copy
import random
# Consider using the modules imported above.

class Hat:

  def __init__(self, **kwargs ):
    self.contents =[]
    
    

    for k,v in kwargs.items():
      for i in range(v):
        self.contents.append(k)
    


  def draw(self, numberOfDraws):
    removedBalls = []

    if numberOfDraws <= int(len(self.contents)):

      for i in range(numberOfDraws):
        selectedItem = random.choice(self.contents)
        removedBalls.append(selectedItem)
        
        #the line below is where the items are getting removed

        self.contents.remove(selectedItem)

      return removedBalls

          
    else:
      return self.contents





def experiment(hat, expected_balls, num_balls_drawn, num_experiments):

  counter = 0

  listFavourableCases = []

  for k,v in expected_balls.items():
    for _ in range(v):
      listFavourableCases.append(k)

  

  for i in range(num_experiments):
    observation = hat.draw(num_balls_drawn)

    if set(observation) == set(listFavourableCases):

      counter += 1

    #print(observation) #to see whats going wrong
  return counter/num_experiments
     

I am unable to restore the original list after a draw, because the hat.draw() method also removes items from the original list (i.e from self.contents).

Use the commented out print statement to see what i mean, pardon any grammatical errors :sweat_smile: :sweat_smile:

One of the methods in here will be helpful.

1 Like

Thanks!! :grin: Finally completed all the cert projects for scientific computing in Python .