Link Dictionary

James Stroud jstroud at mbi.ucla.edu
Mon Jun 11 17:10:43 EDT 2007


anush shetty wrote:
> Hi,
> I have two dictionaries
> 
>  dict1={'B8': set(['I8', 'H8', 'B2', 'B7', 'F8', 'C9', 'G8', 'B4',
> 'B5', 'B6', 'C8', 'E8', 'D8', 'B3', 'A9', 'A8', 'C7', 'B9', 'A7',
> 'B1']), 'B9': set(['I9', 'H9', 'A7', 'F9', 'B3', 'B6', 'G9', 'B4',
> 'B5', 'C9', 'B7', 'E9', 'B1', 'B2', 'D9', 'A9', 'A8', 'C8', 'B8',
> 'C7']), 'D1': set(['F1', 'F2', 'F3', 'G1', 'I1', 'D2', 'H1', 'A1',
> 'D4', 'B1', 'D8', 'D9', 'D6', 'D7', 'C1', 'D5', 'E1', 'D3', 'E3',
> 'E2'])}
> 
> and
> dict2=
> {'I6': '0', 'H9': '9', 'I2': '0', 'E8': '0', 'H3': '0', 'H7': '0',
> 'I7': '3', 'I4': '0', 'H5': '0', 'F9': '0', 'G7': '5', 'G6': '9',
> 'G5': '0', 'E1': '7', 'G3': '2', 'G2': '0', 'G1': '0', 'I1': '0',
> 'C8': '0', 'I3': '5', 'E5': '0', 'I5': '1', 'C9': '0', 'G9': '0',
> 'G8': '0', 'A1': '0', 'A3': '3', 'A2': '0', 'A5': '2', 'A4': '0',
> 'A7': '6', 'A6': '0', 'C3': '1', 'C2': '0', 'C1': '0', 'E6': '0',
> 'C7': '4', 'C6': '6', 'C5': '0', 'C4': '8', 'I9': '0', 'D8': '0',
> 'I8': '0', 'E4': '0', 'D9': '0', 'H8': '0', 'F6': '8', 'A9': '0',
> 'G4': '6', 'A8': '0', 'E7': '0', 'E3': '0', 'F1': '0', 'F2': '0',
> 'F3': '6', 'F4': '7', 'F5': '0', 'E2': '0', 'F7': '2', 'F8': '0',
> 'D2': '0', 'H1': '8', 'H6': '3', 'H2': '0', 'H4': '2', 'D3': '8',
> 'B4': '3', 'B5': '0', 'B6': '5', 'B7': '0', 'E9': '8', 'B1': '9',
> 'B2': '0', 'B3': '0', 'D6': '2', 'D7': '9', 'D4': '1', 'D5': '0',
> 'B8': '0', 'B9': '1', 'D1': '0'}
> 
> Now I want to create a dict which would have both the keys and values
> to be of the corresponding values of dict2.
> 
> Something like this:
> 
> Eg. The first key in dict1 i.e. B8 as 0 (0 is the value of B8 in
> dict2) mapped as set(['0','0','0',...]).
> 
> Can anyone help me out with this.
> -
> Anush
> 

new_dict = {}
for akey, aset in dict1.items():
   new_dict[akey] = sum(int(dict2[k]) for k in aset)

James



More information about the Python-list mailing list