#!/usr/bin/python3
#-*- coding: utf-8 -*-
#-*- author:zhangjiao -*-
'''
自己定義一個字典,完成字典相加的功能與相減的功能,
相加:求並集[若key出現重複就覆蓋]
相減:求交集
__sub__
'''
class dict():
def __init__(self,value):
self.value=value
def __add__(self, other):
print(self.value)
print(other.value)
self.value.update(other.value) #字典的update函數是沒有返回值的
return self.value
# return "dd"
def __sub__(self, other):
print(self.value)
print(other.value)
c={}
for a in self.value:
for b in other.value:
if a==b:
c[a]=other.value[b]
return c
return self.value
a=dict({1:2,3:4})
b=dict({1:3,4:5})
c=a+b
print(c)
print()
c=a-b
print(c)
qfxx第三週3作業
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.