Test if dict contained in dict

后端 未结 4 1252
一向
一向 2020-12-24 11:19

Testing for equality works fine like this for python dicts:

first  = {\"one\":\"un\", \"two\":\"deux\", \"three\":\"trois\"}
second = {\"one\":\"un\", \"two\         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-24 12:17

    all(k in second and second[k] == v for k, v in first.items())
    

    if you know that none of the values can be None, it will simplify to:

    all(second.get(k, None) == v for k, v in first.items())
    

提交回复
热议问题