python中如何通过一个封装函数将一个指定的实例删除

class Company:
number = 0

def __init__(self,comName,comPreporty):
    self.comName = comName
    self.comPreporty = comPreporty
    self.emList = {"name":[],"age":[],"cost":[]}
    Company.number = Company.number +1

def get_company(self):
    print(format(self.comName,self.pro))

def recruit(self,empName,age,cost =1000):
    self.empName = empName
    self.age = age
    self.cost = cost
    self.emList["name"].append(empName)
    self.emList["age"].append(age)
    self.emList["cost"].append(cost)

def getEmployeeList(self):
    return(self.emList)

c1 =Company("AA","BBBB")

c1.recruit("aa",19)
c1.recruit("bb",16)
c1.recruit("cc",14)
c1.recruit("dd",13)
c1.recruit("ee",12)

    我需要用一个叫delete的函数将cc的数据删除
    比如
    c1.delelte("cc")
    然后CC的名字和年龄就删除了,这个功能该如何实现呢?

2个回答

如果问题解决,请点下采纳。

还有之前的问题:https://ask.csdn.net/questions/766740

谢谢

# -*- coding: UTF-8 -*-

class Company:
    number = 0
    def __init__(self,comName,comPreporty):
        self.comName = comName
        self.comPreporty = comPreporty
        self.emList = {"name":[],"age":[],"cost":[]}
        Company.number = Company.number +1

    def get_company(self):
        print(format(self.comName,self.pro))

    def recruit(self,empName,age,cost =1000):
        self.empName = empName
        self.age = age
        self.cost = cost
        self.emList["name"].append(empName)
        self.emList["age"].append(age)
        self.emList["cost"].append(cost)

    def delete(self,empName):
        i = self.emList["name"].index(empName)
        self.emList["name"].remove(self.emList["name"][i])
        self.emList["age"].remove(self.emList["age"][i])
        self.emList["cost"].remove(self.emList["cost"][i])

    def getEmployeeList(self):
        return(self.emList)

c1 = Company("AA","BBBB")
c1.recruit("aa",19)
c1.recruit("bb",16)
c1.recruit("cc",14)
c1.recruit("dd",13)
c1.recruit("ee",12)
print(c1.getEmployeeList())
c1.delete("cc")
print(c1.getEmployeeList())

{'age': [19, 16, 14, 13, 12], 'cost': [1000, 1000, 1000, 1000, 1000], 'name': ['aa', 'bb', 'cc', 'dd', 'ee']}
{'age': [19, 16, 13, 12], 'cost': [1000, 1000, 1000, 1000], 'name': ['aa', 'bb', 'dd', 'ee']}

可以考虑python的析构函数__del__

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!