有没有办法搜索字典并让它返回包含搜索变量的所有PK

时间:2016-05-11 19:43:54

标签: python-2.7 dictionary

我到处都看了,我找不到一个好方法去做我想要做的事......目标是搜索员工的字典数组,搜索名为“”的关键值积极的“这是一个真/假的价值,我有员工词典的方式如下:

Employee{Empl_Num{"Fname":"John", "LName":"Smith", "Phone":"555-555-5555", "Active":True, "isManager":False}}

我如何通过迭代dict并将Employee dict中的Items复制到Inactive Employee dict来进行搜索:以下是我的代码::

def Remove_Empl(self):
  #flips is active switch to inactive.#
  global Employee, EmplNum
  self.Prnt_Empl_Lst()
  Termed=int(raw_input("Please enter the employee number of the employee being termed: "))
  isValid=self.ValidateEmpl(Termed) #ensure a number entered exists
  if isValid==True: # is a valid employee number
     Employee[Termed]["Active"]=False #set active status == False
     self.get_Inact_Empl() #iterates through entire list of employee and checks active status
  else:
     print "No such employee exists, please check the employee list and try again."
  AdminMenu()



def get_Inact_Empl(self):
      global Employee, EmplInactive
      for InvlEmpl in Employee.keys(): #every employee number is searched...
         if Employee[InvlEmpl]["Active"]==False and EmplInactive.has_key(InvlEmpl): #if they are not active, but in array already should skip
            pass
         elif Employee[InvlEmpl]["Active"]==False and EmplInactive.has_key(InvlEmpl)==False:#if they are not active, but nit in array, add to array 
            EmplInactive[InvlEmpl]=Employee[InvlEmpl]
      for Empl in EmplInactive.keys(): ##Prints values in a neat list
         print str(Empl) +": ",
         for Empl1 in EmplInactive[Empl].keys():
            print EmplInactive[Empl][Empl1],
         print ""

奇妙的事实是上面的代码有效......但是刚才我在使用以下内容添加一个名为公司的员工:

def Add_Empl(self):
  global EmplNum, Employee,EmplInactive
  FirstName=raw_input("Please enter employees first name: ")
  LastName=raw_input("Please enter employees last name: ")
  Phone=raw_input("Please enter employees phone number: ")

  for IE_Key in EmplInactive.keys():
     if EmplInactive[IE_Key]["FirstName"]==FirstName and EmplInactive[IE_Key]["LastName"]==LastName and EmplInactive[IE_Key]["Phone"]==Phone:
        del EmplInactive[IE_Key]
        Employee[IE_Key]["Active"]=True
        break

  ##if there is no value then create a new entry##
  EmplNum+=1      
  Employee[EmplNum]={"FirstName":FirstName,"LastName":LastName,"Phone":Phone, "Active":True, "isManager":False} ## read following block comment on this##

  AdminMenu()
  '''
  Pretty much most is self explanitory, employee name, and phone, may add address here too... Important parts are the last three, Logged in#not yet added#
  Active meaning that the employee is a member of the corporation and not been termed either voluntary or involuntary. isManager is the designation that
  attaches employee to managers que, as a manager additional options become avail if logged in... employee and manager staff had seperate log ins and may
  proocess transactions as employee or as manager depending on log in status. Removal of employees can be done by manager level staff, However only marks
  Active status false. can be reactivated later date if needs be. DEFAULTS: Active=TRUE IsManager=FALSE LoggedIn=FALSE*when applied
  '''

它做了它应该做的事情,但它生成了一个新的员工因此在字典中的副本,新的数字和空间...... :(如果在非活动数组中没有值,我试图放置该部分用户,它为每个不匹配搜索的密钥的非活动用户创建一个新员工,或者只创建一个附加条目...但它仍然创建了太多额外的条目...

请帮助......:'(

Jesse Fender

0 个答案:

没有答案