Python为每个输入分隔字典

时间:2017-10-30 17:31:58

标签: python python-3.x dictionary

目前我的函数将每个员工输入添加到一个字典中,但我想让它成为每个员工都是其OWN字典。然后我将所有这些词典添加到全局词典中。我完全卡住了,所以一切都有帮助!

#Function for adding employee information
def add_emp():
    #Ask the user to add an employee
    print("Enter the employee's information:\n")
    #Input first and last name
    name = input("What is the employee's name? ").lower()
    #Input employee position
    position = input("What is their position? ").lower()
    #Input employee department
    em_department = input("What is their department? ").lower()
    #Input employee salary
    salary = float(input("What is their salary? "))
    #Add information to a dictionary called employees
    em_list = [position, em_department, salary]
    employees[name] = em_list
    #Add dictionary to global dictionary of all employees
    em_global["Employee"] = employees

3 个答案:

答案 0 :(得分:3)

您需要将员工自己的dictionary详细信息添加到global dictionary - em_global

因此,如果我们有以下修改后的code

em_global = {}

def add_emp():
    #Ask the user to add an employee
    print("Enter the employee's information:\n")

    #Input first and last name
    name = input("What is the employee's name? ").lower()
    #Input employee position
    position = input("What is their position? ").lower()
    #Input employee department
    em_department = input("What is their department? ").lower()
    #Input employee salary
    salary = float(input("What is their salary? "))

    #create local employee dictionary
    em_dict = {'position': position,
               'em_department': em_department,
               'salary': salary}

    #Add dictionary to global dictionary of all employees
    em_global[name] = em_dict

add_emp()

然后如果我们运行它并输入以下信息:

Enter the employee's information:

What is the employee's name? bob
What is their position? manager
What is their department? coding
What is their salary? 100.99

然后em_global dictionary现在变为:

{'bob': {'position': 'manager', 'salary': 100.99, 'em_department': 'coding'}}

答案 1 :(得分:2)

从代码中可以看出,您已经拥有一个名为employees的字典和另一个字典em_global。目前,您正在构建一个包含员工详细信息的列表,您可以更改该列表以创建一个词典。然后你可以将它添加到em_global [" Employee"]这将是一个词典列表。它会对你有用吗?

  em_global={}
  em_global["Employee"] = []
  def add_emp():
    #Ask the user to add an employee
    print("Enter the employee's information:\n")
    #Input first and last name
    name = input("What is the employee's name? ").lower()
    #Input employee position
    position = input("What is their position? ").lower()
    #Input employee department
    em_department = input("What is their department? ").lower()
    #Input employee salary
    salary = float(input("What is their salary? "))
    #Add information to a dictionary called employees
    em_list={'name':name,'position':position, 'department':em_department, 'salary':salary}
    # employees[name] = em_list
    #Add dictionary to global dictionary of all employees
    em_global["Employee"].append(em_list)

答案 2 :(得分:1)

您可以将数据保存在json文件中,而不是使用标准输入来输入员工数据,而是以最有效的方式将其导入python中。例如,创建一个像这样的json文件:

{'employee1': {'em_department': 'dept1', 'position': 'pos1', 'salary': 19999}, 'employee2': {'em_department': 'dept2', 'position': 'pos2', 'salary': 29999}}

然后在python中导入此文件:

import json
FP = open('filename')
em_global = json.load(FP)

现在你有了一个全球词典em_global,其中每个员工依次是一个包含部门,职位和薪水的字典。 例如,要访问employee2的工资,你可以

em_global['employee2']['salary']