There are 3 dictionaries: inventData , invent101 and invent21 , which I invent21 together, making 1 of them - inventDataAll .

Although the dictionaries are connected, the fact is that the inventData dictionary contains data that is not in invent101 and invent21 . I need the script to print what data it did not find, but this does not happen.

Instead, the "Inventory" + value ['invent'] + "invoice does not appear" appears exactly as many times as the first cycle works, regardless of whether this data is in invent101 and invent21 or not.

I wanted to get help on this.

 inventData = {} # 1 словарь # ... inventData.setdefault(index, dict(name=name, serial=serial, invent=invent, produce=produce)) # ... invent101 = {} # 2 словарь # ... invent101.setdefault(index, dict(invent=invent, checked=checked, worked=worked, cost=cost)) # ... invent21 = {} # 3 словарь # ... invent21.setdefault(index, dict(invent=invent, checked=checked, worked=worked, cost=cost)) # ... inventDataAll = {} # Итоговый словарь for key, value in inventData.items(): for key101, value101 in invent101.items(): if value['invent'] == value101['invent']: inventDataAll.setdefault(key, dict(name=value['name'], serial=value['serial'], invent=value['invent'], produce=value['produce'], checked=value101['checked'], worked=value101['worked'], cost=value101['cost'])) elif value['invent'] != value101['invent']: for key21, value21 in invent21.items(): if value['invent'] == value21['invent']: inventDataAll.setdefault(key, dict(name=value['name'], serial=value['serial'], invent=value['invent'], produce=value['produce'], checked=value21['checked'], worked=value21['worked'], cost=value21['cost'])) else: print("Инвентарник" + value['invent'] + "счетах не значится") 
  • As far as I understand, else should be on the same level with if, and now it is on the same level with the internal for, so the code in this else works every time the internal for completes successfully. - insolor
  • Show the structure of the dictionaries. Perhaps there is a more elegant way to find discrepancies. - Sergey Gornostaev
  • Dear @Sergey Gornostaev on account of the structure of the dictionaries, the data is taken from 3 ekselevskih files, long thought how to combine the data, only thought of this. - Pylkey
  • As for the comment of a friend, @insolor esle rearranged more than once in all sorts of variations, he dabbled with the brake, nothing good comes out. I understood what you mean , but it seems to me that everything I could have tried - Pylkey
  • @Sergey Gornostaev structure indicated above. - Pylkey 5:53 pm

1 answer 1

Instead, the "Inventory" + value ['invent'] + "invoice does not appear" appears exactly as many times as the first cycle works, regardless of whether this data is in invent101 and invent21 or not.

else you refer to nested for . In this case, else is called if the break inside for not called. Intention to instead:

 found = False for hay_or_needle in haystack: if hay_or_needle == needle: found = True break if not found: print("no needle in the haystack") 

writing is simple:

 for hay_or_needle in haystack: if hay_or_needle == needle: break # found else: print("no needle in the haystack") 

Note: else under for is, not under if .

I do not see break in a nested loop, so the code from else is executed during each pass in the outer loop.


What your code does is hard to understand. Minimal change: remove the else at the end and use: if key not in inventDataAll instead.

You can put a repeating code into a function (not tested):

 from operator import itemgetter def dict_subset(mapping, keys): return dict(zip(keys, itemgetter(*keys)(mapping))) # merge (values from invent101 overwrite invent21) inv = {v['invent']: v for d in [invent21, invent101] for v in d.values()} inventDataAll = {} for key, value in inventData.items(): if value['invent'] in inv: inventDataAll[key] = { **dict_subset(value, 'name serial invent produce'.split()), **dict_subset(inv[value['invent']], 'checked worked cost'.split())} else: print("Инвентарник {[invent]} счетах не значится".format(value)) 

See if this will not look clearer as SQL (you can db = sqlite3.connect(':memory:') ).