Home>
Be in trouble

If I have the following data, I would like to delete the dictionary whose key and value match exactly, but I don't know how to write the code. Could anyone please teach me? We apologize for the inconvenience, but thank you.

I want to achieve

Data (before processing)

data = {'test1': {'aaa': 29,'eee': 40},
      'test2': {'aaa': 5,'bbb': 10},
      'test3': {'aaa': 29,'ccc': 10},
      'test4': {'aaa': 17,'bbb': 50},
      ...
      'test998': {'aaa': 5,'bbb': 10},
      'test999': {'aaa': 29,'bbb': 40},
     'test1000': {'aaa': 17,'bbb': 50}}


After treatment

new_data = {'test1': {'aaa': 29,'eee': 40},
      'test2': {'aaa': 5,'bbb': 10},
      'test3': {'aaa': 29,'ccc': 10},
      'test4': {'aaa': 17,'bbb': 50},
      ...
      'test999': {'aaa': 29,'bbb': 40}}
  • Answer # 1

    For example, is it like this?

    data = {'test1': {'aaa': 29,'eee': 40},
          'test2': {'aaa': 5,'bbb': 10},
          'test3': {'aaa': 29,'ccc': 10},
          'test4': {'aaa': 17,'bbb': 50},
          'test998': {'aaa': 5,'bbb': 10},
          'test999': {'aaa': 29,'bbb': 40},
         'test1000': {'aaa': 17,'bbb': 50}}
    new_data = {}
    for key, value in data.items ():
        if value not in new_data.values ​​():
            new_data [key] = value
    print (new_data)
    # {'test1': {'aaa': 29,'eee': 40},
    #'test2': {'aaa': 5,'bbb': 10},
    #'test3': {'aaa': 29,'ccc': 10},
    #'test4': {'aaa': 17,'bbb': 50},
    #'test999': {'aaa': 29,'bbb': 40}}