Home > Enterprise >  Check if items in one dictionary are absent from another
Check if items in one dictionary are absent from another

Time:02-26

I have two dictionaries:

d ={0: {'spieltag': '1. Spieltag:', 'tag': 'So', 'd_u': '15.08.2021 17:30', 'team1': '1. FC Köln', 'team2': 'Hertha BSC Berlin', 'h_tore': '3', 'a_tore': '1', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '1', 'absage': ' '},
    1: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'Arminia Bielefeld', 'team2': 'SC Freiburg', 'h_tore': '0', 'a_tore': '0', 'halbzeit_h_tore': '0', 'halbzeit_a_tore': '0', 'absage': ' '},
    2: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'VfL Wolfsburg', 'team2': 'VfL Bochum', 'h_tore': '1', 'a_tore': '0', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '0', 'absage': ' '},
    3: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'FC Augsburg', 'team2': 'TSG Hoffenheim', 'h_tore': '0', 'a_tore': '4', 'halbzeit_h_tore': '0', 'halbzeit_a_tore': '1', 'absage': ' '},
    4: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'VfB Stuttgart', 'team2': 'Greuther Fürth', 'h_tore': '5', 'a_tore': '1', 'halbzeit_h_tore': '2', 'halbzeit_a_tore': '0', 'absage': ' '},
    5: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': '1. FC Union Berlin', 'team2': 'Bayer Leverkusen', 'h_tore': '1', 'a_tore': '1', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '1', 'absage': ' '},
    6: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 18:30', 'team1': 'Borussia Dortmund', 'team2': 'Eintracht Frankfurt', 'h_tore': '5', 'a_tore': '2', 'halbzeit_h_tore': '3', 'halbzeit_a_tore': '1', 'absage': ' '},
    7: {'spieltag': '1. Spieltag:', 'tag': 'So', 'd_u': '15.08.2021 15:30', 'team1': 'FSV Mainz 05', 'team2': 'RB Leipzig', 'h_tore': '1', 'a_tore': '0', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '0', 'absage': ' '},
    8: {'spieltag': '1. Spieltag:', 'tag': 'Fr', 'd_u': '13.08.2021 20:30', 'team1': 'Borussia M´gladbach', 'team2': 'Bayern München', 'h_tore': '1', 'a_tore': '1', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '1', 'absage': ' '}}

m ={0: {'Spieltag': '3', 'tag': 'Fr', 'd_u': '27.08.2021 20:30', 'team1': 'Borussia Dortmund', 'team2': 'TSG Hoffenheim', 'h_tore': '3', 'a_tore': '2', 'halbzeit_h_tore': '0', 'halbzeit_a_tore': '0', 'absage': ' '},
    1: {'Spieltag': '3', 'tag': 'Sa', 'd_u': '28.08.2021 15:30', 'team1': 'VfB Stuttgart', 'team2': 'SC Freiburg', 'h_tore': '-', 'a_tore': '-', 'halbzeit_h_tore': '-', 'halbzeit_a_tore': '-', 'absage': ' '},
    2: {'Spieltag': '3', 'tag': 'Sa', 'd_u': '28.08.2021 15:30', 'team1': 'Arminia Bielefeld', 'team2': 'Eintracht Frankfurt', 'h_tore': '1', 'a_tore': '1', 'halbzeit_h_tore': '0', 'halbzeit_a_tore': '1', 'absage': ' '},
    3: {'Spieltag': '3', 'tag': 'Sa', 'd_u': '28.08.2021 15:30', 'team1': '1. FC Köln', 'team2': 'VfL Bochum', 'h_tore': '2', 'a_tore': '1', 'halbzeit_h_tore': '0', 'halbzeit_a_tore': '0', 'absage': ' '},
    4: {'Spieltag': '3', 'tag': 'Sa', 'd_u': '28.08.2021 15:30', 'team1': 'FC Augsburg', 'team2': 'Bayer Leverkusen', 'h_tore': '1', 'a_tore': '4', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '2', 'absage': ' '},
    5: {'Spieltag': '3', 'tag': 'Sa', 'd_u': '28.08.2021 15:30', 'team1': 'FSV Mainz 05', 'team2': 'Greuther Fürth', 'h_tore': '3', 'a_tore': '0', 'halbzeit_h_tore': '2', 'halbzeit_a_tore': '0', 'absage': ' '},
    6: {'Spieltag': '3', 'tag': 'Sa', 'd_u': '28.08.2021 18:30', 'team1': 'Bayern München', 'team2': 'Hertha BSC Berlin', 'h_tore': '5', 'a_tore': '0', 'halbzeit_h_tore': '2', 'halbzeit_a_tore': '0', 'absage': ' '},
    7: {'Spieltag': '3', 'tag': 'So', 'd_u': '29.08.2021 15:30', 'team1': '1. FC Union Berlin', 'team2': 'Borussia M´gladbach', 'h_tore': '2', 'a_tore': '1', 'halbzeit_h_tore': '2', 'halbzeit_a_tore': '0', 'absage': ' '},
    8: {'Spieltag': '3', 'tag': 'So', 'd_u': '29.08.2021 17:30', 'team1': 'VfL Wolfsburg', 'team2': 'RB Leipzig', 'h_tore': '-', 'a_tore': '-', 'halbzeit_h_tore': '-', 'halbzeit_a_tore': '-', 'absage': ' '}}

For each item in d, how can I check if the item is absent in m and if absent perform some operation?

CodePudding user response:

Note that none of the values (dictionaries) in d have a match in m. Here is some simpler data with a matching value:

d = {
  1: {'type': 'star', 'name': 'Sun'},
  2: {'type': 'natural satellite', 'name': 'Moon'}
}

m = {
  1: {'type': 'natural satellite', 'name': 'Moon'},
  2: {'type':'planet', 'name': 'Earth'}
}

Solution:

for item in d.items():
  if item not in m.items(): do_operation()

Remember that the not in operator does comparisons internally.

  • Related