简体   繁体   English

在Python中向现有字典添加新键值

[英]Adding new key value to existing dictionary in Python

i have created a dictionary in Python. 我用Python创建了一个字典。

Employee = dict(EmpName="Peter", Empid="456", Designation="Country Head") 

now if i want to add to same dictionary something like this. 现在,如果我想将相同的内容添加到同一本词典中。

Employee.update(EmpName="Suresh", Empid="585", Designation="Director")

Please suggest. 请提出建议。 i am trying to add another element in same dictionary. 我试图在同一词典中添加另一个元素。 but it is not getting added. 但它没有被添加。

You can simply do this: 您可以简单地做到这一点:

Employee = dict(EmpName="Peter", Empid="456", Designation="Country Head")
...
Employee = dict(EmpName="Suresh", Empid="585", Designation="Director")

And, if you want to have an update() method, then create a class Employee which contains the update() method. 并且,如果要使用update()方法,则创建一个包含update()方法的Employee类。

Anyway, as the fields seems to be fixed, I would advise you to use an object Employee with the fields EmpName , Empid and Designation in it. 无论如何,由于这些字段似乎是固定的,因此我建议您使用其中包含EmpNameEmpidDesignation字段的Employee对象。

One of the major benefit of using an object (in place of several dictionaries objects), is that you will be able to type (and, then, trust the content of each element) of a collection of Employee 's objects. 使用对象(代替几个字典对象)的主要好处之一是,您将能够键入(然后信任每个元素的内容)一组Employee对象。 Where, manipulating a collection of dictionaries may just lead you to have missing fields or different labels for each fields. 在哪里操作词典集合可能只会导致您缺少字段或每个字段使用不同的标签。

It seems to me you want a table of employees with 3 attribute columns, name , id , and designation . 在我看来,你想用3属性列, 姓名身份证 ,并指定员工的表。 So your data has 2 dimensions, employees vs attributes. 因此,您的数据具有2个维度,即员工vs属性。 You can't do that with a single dictionary. 您无法使用一个字典来做到这一点。

One way to do what I think you want is this. 一种我认为想要的方法是这种方法。 Start by defining the row structure with attributes. 首先定义具有属性的行结构。

>>> from collections import namedtuple
>>> Employee = namedtuple('Employee','EmpName,Empid,Designation')

Now build the employee dimension. 现在建立员工维度。

>>> employees = []
>>> employees.append(Employee(EmpName="Peter", Empid="456", Designation="Country Head"))
>>> employees.append(Employee(EmpName="Suresh", Empid="585", Designation="Director"))
>>> employees
[Employee(EmpName='Peter', Empid='456', Designation='Country Head'), Employee(EmpName='Suresh', Empid='585', Designation='Director')]

Although this is what I think you want, I have to point out that it is not a very useful data structure for further processing. 尽管这是我想要的,但我必须指出,它不是用于进一步处理的非常有用的数据结构。 For example, updates are difficult. 例如,更新很困难。 Suppose Suresh gets promoted to Managing Director. 假设Suresh升任董事总经理。 The only way to update this data structure to reflect that is to search through all the employees to find the one you want to update. 更新此数据结构以反映该数据结构的唯一方法是搜索所有员工,以找到要更新的员工。 Or suppose an employee resigns: ditto. 或假设一名雇员辞职:同上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM