Python 2.7 通过键入a+实现集合并集;其中a和b是两本词典
我想用python中的类和字典实现python中的内置数据类型集。我已经包含了一些基本函数,但是我不能执行在上面定义的并集和交集操作。我只想写c=a+b,其中a和b是两个字典c是另一个字典,它的键给出了“a”和“b”的并集。我尝试了try和,除了下面的代码中给出的,但是我想要一个更好的解决方案。有人能帮我吗Python 2.7 通过键入a+实现集合并集;其中a和b是两本词典,python-2.7,Python 2.7,我想用python中的类和字典实现python中的内置数据类型集。我已经包含了一些基本函数,但是我不能执行在上面定义的并集和交集操作。我只想写c=a+b,其中a和b是两个字典c是另一个字典,它的键给出了“a”和“b”的并集。我尝试了try和,除了下面的代码中给出的,但是我想要一个更好的解决方案。有人能帮我吗 class My_Set: def __init__(self,listt): if listt: self.d
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()
您希望它包含哪些值?也许有用,如果a={1:2,3:4}和b={6:7,6:7},那么c应该是{1:2,3:4,4:5,6:7}如果您询问集合可以包含的值,那么集合可以具有python集合中内置支持的任何数据类型