python在嵌套字典中附加字典

时间:2018-05-05 07:03:29

标签: python

我有这本词典:

appointment = { soccer = {
                           day : 20,
                           month : april
                          }
               }

如何在同一结构中插入此dictio?

appointment = { gym = {
                           day : 5,
                           month : may
                          }
               }

预期产出:

appointment = { soccer = {
                           day : 20,
                           month : april
                          },
                 gym = {
                           day : 5,
                           month : may
                          }
               }

如何通过代码连接此dictio?

3 个答案:

答案 0 :(得分:1)

如果您使用的是python 3.5或更高版本,则可以使用以下语法合并字典:

 public async Task<ActionResult> RegisterPatient(RegisterViewModel model)
    {

        MYDb db = new MYDb();
        Patient pt = new Patient();
        pt.BirthDate = model.BirthDate;
        pt.City = model.City;
        pt.Country = model.Country;
        pt.ProfilePicture = model.ProfilePicture;
        pt.FName = model.FName;
        int Pat_id = model.PatientID;
        if (ModelState.IsValid)
        { 
            ApplicationUser myuser = new ApplicationUser();
            model.CreatedOn = DateTime.Now;
            model.IsActive = true;
            myuser.PatientID = Pat_id;
            if (ModelState.IsValid)
            {
                var user = model.GetUser();
                db.Patients.Add(pt);
                db.SaveChanges();
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                { 
                    var idManager = new IdentityManager();
                    idManager.AddUserToRole(user.Id, model.User_type);
                    //await db.SaveChangesAsync();
                    await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

            return RedirectToAction("Index", "Home");
        }

        // If we got this far, something failed, redisplay form
        return View();
    }

答案 1 :(得分:0)

如果dicts已存在,您可以查看dict.update()

简短的回答是:

>>> appointment = { 'soccer' : { 'day': 20, 'month': 'april' } }
>>> appointment2 = { 'gym' : { 'day': 5, 'month': 'may' } }
>>> appointment.update(appointment2)
>>> appointment
{'gym': {'day': 5, 'month': 'may'}, 'soccer': {'day': 20, 'month': 'april'}}

但是如果你正在制作这些词汇,那么以通常的方式添加新词条会更有意义(参见:Add new keys to a dictionary?):

>>> appointment = { 'soccer' : { 'day': 20, 'month': 'april' } }
>>> appointment['gym'] = {'day': 5, 'month': 'may'}
>>> appointment
{'gym': {'day': 5, 'month': 'may'}, 'soccer': {'day': 20, 'month': 'april'}}

答案 2 :(得分:0)

我认为您正在寻找appointment = { 'soccer' : { 'day' : 20, 'month' :'april' }} appointment2 = {'gym': {'day': 20, 'month': 'april'}} # update appointment with appointment2 values appointment.update(appointment2) {'soccer': {'day': 20, 'month': 'april'}, 'gym': {'day': 20, 'month': 'april'}} 方法

  

D.update([E,] ** F) - &gt;无。

     

从dict / iterable E和F。

更新D.      

如果E存在且具有.keys()方法,那么:对于E中的k:D [k] = E [k]

     

如果E存在且缺少.keys()方法,那么:对于k,v在E中:D [k] = v

     

在任何一种情况下,接着是:对于F中的k:D [k] = F [k]

你的情况,

appointment2

appointment变量更新到位时,// test.js it('delete user', async () => { const userByEmail = await request(app) .get('/users/get-by-email/user@test') .set('Accept', 'application/json') .set('Authorization', config.get('test.tokens.admin')); const result = await testIt('delete', `/users/${userByEmail.body._id}`); expect(result).toEqual([401, 403, 401, 200]); });变量保持不变。