我有两个字典,一个包含一些寄存器的名称和初始值,另一个包含这些寄存器的名称和地址值。
我需要比较寄存器名称[id]上的两个字典,然后从字典1中获取寄存器[initValue]的初始值,从字典2中获取地址值[addrValue],并将它们放入新字典中,以便这个新词典中的键成为地址而不是寄存器名称。
以下是我正在做的事情,但我不知道如何合并两个字典键。
regex = "(?P<id>\w+?)_INIT\s*?=.*?'h(?P<initValue>[0-9a-fA-F]*)"
for 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]*)"
for y in re.findall(regex, lines):
addr_list = (y.groupdict()["addr_id"], "0x" + y.groupdict()["addrValue"])
for 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'] # something here
答案 0 :(得分:2)
为什么不在一次传递中构建一个单独的dict?
from collections import defaultdict
r_line = re.compile(r"(?P<id>\w+?)_(?P<type>[A-Z]+)\s*?=.*?'h(?P<value>[0-9a-fA-F]*)")
register_ids = defaultdict(dict)
for match in r_line.finditer(lines):
groups = match.groupdict()
key = groups['type'].tolower()
# store as int. You can use `hex()` or format codes for display as hex
value = int(groups['value'], 16)
register_ids[groups['id']][key] = value
如果您有多组线路,这不是问题:它只会使用新数据更新register_ids
:
for lines in lines_list:
for match in r_line.finditer(lines):
...
这将生成一个如下所示的单个词典ids
:
{'register1': {'addr': 1234, 'init': 5678, ...}, 'register2': {...}, ...}
然后,您可以非常轻松地生成任何其他类型的派生词典。例如,对于由地址键入的初始值:
addresses = {}
for values in register_ids.itervalues():
try:
addresses[values['addr']] = values['init']
except KeyError:
pass
答案 1 :(得分:1)
我不确定代码与您的问题有什么关系。
但是我们假设你有两个词。 dictA
和dictB
(以下是示例说明):
>>> dictA = dict.fromkeys(range(10), {'initial_val_1': 'value_a', 'initial_val_2': 'value_b' })
>>> dictB = dict.fromkeys(range(5, 15), {'address': 'my_address'})
您需要的代码:
>>> from collections import defaultdict
>>> shared_keys = set(dictA.keys()).intersection(set(dictB.keys()))
>>> combined = defaultdict(dict)
>>> for key in shared_keys:
combined[key].update(dictA[k])
combined[key].update(dictB[k])
查看字典:
>>> for k, v in combined.items():
print k, v
5 {'initial_val_2': 'value_b', 'initial_val_1': 'value_a', 'address': 'my_address'}
6 {'initial_val_2': 'value_b', 'initial_val_1': 'value_a', 'address': 'my_address'}
7 {'initial_val_2': 'value_b', 'initial_val_1': 'value_a', 'address': 'my_address'}
8 {'initial_val_2': 'value_b', 'initial_val_1': 'value_a', 'address': 'my_address'}
9 {'initial_val_2': 'value_b', 'initial_val_1': 'value_a', 'address': 'my_address'}
答案 2 :(得分:1)
制作前两个词典:
dict1 = {}
dict2 = {}
for x in range(0, 10):
dict1[x] = "initValue{}".format(x)
y = x+3
dict2[y] = "address{}".format(y)
根据dict2“地址”
组合它们dict3 = {}
#Note because you want the addresses to be the key of the new dictionary
#we only need to look at the keys that exist in both dict1 and dict2.
for key in dict2.keys():
if key in dict1:
dict3[dict2[key]] = dict1[key]
字典:
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'}