fatesses
2019-07-13 11:50
采纳率: 98%
浏览 441

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条回答 默认 最新

  • threenewbee 2019-07-13 13:21
    已采纳

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

    还有之前的问题: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']}

    打赏 评论
  • 超超92 2019-07-16 23:44

    可以考虑python的析构函数__del__

    打赏 评论

相关推荐 更多相似问题