5

I want to implement the built in data type set in python using a class and dictionary in python. I have included certain basic functions, but i could not perform the union and intersection operations defined on it. I wish to just write c=a+b where a and b are two dictionaries c is yet another dictionary whose keys give the union of 'a' and 'b'. I tried with try and except as given in my code below, but i want a better solution. can anyone help me with this?

class My_Set:
      def  __init__(self,listt):
            if listt:
                    self.dictionary={}
                    i=0
                    for x in listt:
                            self.dictionary[x]=len(x)
                            i=i+1
            else:
                    self.dictionary={}

    def is_element(self,element):
            if element in self.dictionary:
                    return True
            else:
                    return False

    def remove(self,element):
            if element in self.dictionary:
                    self.dictionary.pop(element)
            else:
                    print 'element missing'
    def add_element(self,element):
            self.dictionary.update({element:len(element)})
            #return self.dictionary
    def union(self,other):
            self.dictionary.update(other.dictionary)
            return self.dictionary.keys()
Chaythanya Nair
  • 4,774
  • 6
  • 32
  • 40

0 Answers0