python - Compare two dictionaries with one key in common and form a new dictionary combining the remaining keys associated with this common key -
i have 2 dictionaries, 1 contains name , initial values of registers , other contains name , address values of same registers.
i need compare 2 dictionaries on register names [id] , take initial value of register [initvalue] dictionary 1 , address values [addrvalue] dictionary 2 , put them in new dictionary key in new dictionary becomes address rather register name.
below doing don't know how merge 2 dictionary keys.
regex = "(?p<id>\w+?)_init\s*?=.*?'h(?p<initvalue>[0-9a-fa-f]*)" x in re.findall(regex, lines): init_list = (x.groupdict()["id"], "0x" + x.groupdict()["initvalue"]) regex = "(?p<id>\w+?)_addr\s*?=.*?'h(?p<addrvalue>[0-9a-fa-f]*)" y in re.findall(regex, lines): addr_list = (y.groupdict()["addr_id"], "0x" + y.groupdict()["addrvalue"]) key in set(init_list['init_id'].keys()).union(addr_list['id'].keys()): if init_list[key] == addr_list[key] expect_by_addr = dict (addr_list['addrvalue'] # here
make first 2 dictionaries:
dict1 = {} dict2 = {} x in range(0, 10): dict1[x] = "initvalue{}".format(x) y = x+3 dict2[y] = "address{}".format(y)
combine them based on dict2 "addresses"
dict3 = {} #note because want addresses key of new dictionary #we need @ keys exist in both dict1 , dict2. key in dict2.keys(): if key in dict1: dict3[dict2[key]] = dict1[key]
dictionaries:
dict1 {0: 'initvalue0', 1: 'initvalue1', 2: 'initvalue2', 3: 'initvalue3', 4: 'initvalue4', 5: 'initvalue5', 6: 'initvalue6', 7: 'initvalue7', 8: 'initvalue8', 9: 'initvalue9'} dict2 {3: 'address3', 4: 'address4', 5: 'address5', 6: 'address6', 7: 'address7', 8: 'address8', 9: 'address9', 10: 'address10', 11: 'address11', 12: 'address12'} dict3 (combined) {'address5': 'initvalue5', 'address4': 'initvalue4', 'address7': 'initvalue7', 'address6': 'initvalue6', 'address3': 'initvalue3', 'address9': 'initvalue9', 'address8': 'initvalue8'}
Comments
Post a Comment