python 如图 三道题 第一个写class第二个第三个不要用内置函数是对list 10
展开全部
#你看看 符合要求吗? 我的理解是 mySet是集合,元素不重复的
class mySet:
def __init__(self,mylist):
self.mylist=[]
for i in mylist:
if i not in self.mylist:
self.mylist.append(i)
def addElem(self,i):
if i not in self.mylist:
self.mylist.append(i)
def deleElem(self,i):
if i in self.mylist:
self.mylist.remove(i)
def interse(self,other):
temp=[i for i in self.mylist if i in other.mylist]
return mySet(temp)
def union(self,other):
temp=[i for i in self.mylist ]
for j in other.mylist:
if j not in temp:
temp.append(j)
return mySet(temp)
def subtract(self,other):
temp=[i for i in self.mylist if i not in other.mylist ]
return mySet(temp)
def __add__(self,other):
return self.union(other)
def __sub__(self,other):
return self.subtract(other)
def __string__(self):
return str(self.mylist)
def __repr__(self):
return self.__string__()
def test():
list1=[1,3,5,7]
list2=[2,4,6,8]
list3=[1,4,7,10]
s1=mySet(list1)
s2=mySet(list2)
s3=mySet(list3)
s1.addElem(7)
s2.deleElem(6)
ss=s1+s2
tt=ss-s3
xx=s1.interse(s3)
print ss
print tt
print xx
if __name__=="__main__":
test()
def get_min(mylist):
try:
x=mylist[0]
except:
print "列表为空"
return
for y in mylist:
x= (x<y and [x] or [y])[0]
return x
def get_slice(mylist,start,stop,step=1):
length=len(mylist)
stop=(stop+length)%length
start=(start+length)%length
num=(stop-start)/step
temp=[]
if num>=0:
temp=[mylist[start]]
start+=step
for i in range(num):
if start==stop:break
temp.append(mylist[start])
start+=step
return temp
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询