Reputation: 11469
I would like to create a dictionary with multiple keys and values. At this point I am not sure that I am putting my question correctly. But here is an example of what I want to create :
patDct = {
'mkey1':{'key1':'val_a1', 'key2':'val_a2', 'key3':'val_a3'},
'mkey2':{'key1':'val_b1', 'key2':'val_b2', 'key3':'val_b3'},
....
}
I have two dictionaries and I am pulling information for 'mkey*', and 'val*' from them. 'key*' are strings.
I have a piece of code to create the dictionary without the 'mkey*', but that only prints out the last set of values. Following is what I have now. "storedct" and "datadct" are two given dictionaries. Here I would like 'mkey*' to get the value of "item".
patDct = dict()
for item in storedct :
for pattern in datadct :
if pattern in item :
patDct['key1'] = datadct[pattern]["dpath"]
patDct['key2'] = datadct[pattern]["mask"]
patDct['key3'] = storedct[item]
Thanks for any suggestion.
Upvotes: 0
Views: 362
Reputation: 14209
From what I understand from your code, I guess that:
patDct = dict()
i = 0
for item in storedct :
for pattern in datadct :
if pattern in item :
i = i + 1
new_item = {}
new_item['key1'] = datadct[pattern]["dpath"]
new_item['key2'] = datadct[pattern]["mask"]
new_item['key3'] = storedct[item]
# I used a counter to generate the `mkey` values,
# not sure you want it that way
patDct['mkey{0}'.format(i)] = new_item
should not be far from your needs...
Upvotes: 1
Reputation: 113930
patDct = dict()
n=1
for item in storedct :
patDct["mkey%s"%n] = {}
p = patDct["mkey%s"%n]
for pattern in datadct :
if pattern in item :
p['key1'] = datadct[pattern]["dpath"]
p['key2'] = datadct[pattern]["mask"]
p['key3'] = storedct[item]
n +=1
print patDct
Upvotes: 1