471,334 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,334 software developers and data experts.

Link Dictionary

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

Jun 11 '07 #1
8 1218
En Mon, 11 Jun 2007 17:24:26 -0300, anush shetty
<it************@gmail.comescribió:
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']), ...

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', [...]
'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',...]).
Sets can't have duplicate elements, so set(['0','0','0'])==set(['0'])
And dictionaries can't have duplicate keys either.
What do you want to do exactly? How would your desired structure look like?

--
Gabriel Genellina

Jun 11 '07 #2
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
Jun 11 '07 #3
On Jun 12, 2:10 am, James Stroud <jstr...@mbi.ucla.eduwrote:
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

So there is no way I can represent all the duplicates right.

So is there any solution where I could take the values of dictionary
and represent them separately?

-
Anush

Jun 11 '07 #4
On Jun 12, 1:50 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Mon, 11 Jun 2007 17:24:26 -0300, anush shetty
<itsanushshe...@gmail.comescribió:
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']), ...
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', [...]
'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',...]).

Sets can't have duplicate elements, so set(['0','0','0'])==set(['0'])
And dictionaries can't have duplicate keys either.
What do you want to do exactly? How would your desired structure look like?

--
Gabriel Genellina
Need it for a verification of the puzzle. It has duplicate values. But
there is any way where i could represent them with duplicates

-
Anush

Jun 11 '07 #5
On Jun 11, 10:19 pm, anush shetty <itsanushshe...@gmail.comwrote:
On Jun 12, 2:10 am, James Stroud <jstr...@mbi.ucla.eduwrote:
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

So there is no way I can represent all the duplicates right.

So is there any solution where I could take the values of dictionary
and represent them separately?

-
Anush
I use lists instead of your dict1 but preserve your mapping dict2:

data = [
['B8',
['I8', 'H8', 'B2', 'B7', 'F8',
'C9', 'G8', 'B4', 'B5', 'B6',
'C8', 'E8', 'D8', 'B3', 'A9',
'A8', 'C7', 'B9', 'A7', 'B1']],
['B9',
['I9', 'H9', 'A7', 'F9', 'B3',
'B6', 'G9', 'B4', 'B5', 'C9',
'B7', 'E9', 'B1', 'B2', 'D9',
'A9', 'A8', 'C8', 'B8', 'C7']],
['D1',
['F1', 'F2', 'F3', 'G1', 'I1',
'D2', 'H1', 'A1', 'D4', 'B1',
'D8', 'D9', 'D6', 'D7', 'C1',
'D5', 'E1', 'D3', 'E3', 'E2']]]

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'}

dout = [[dict2[i],[dict2[k] for k in j]]
for i,j in data]
print dout

- Paddy.

Jun 11 '07 #6
>
dout = [[dict2[i],[dict2[k] for k in j]]
for i,j in data]
print dout

- Paddy.
Working from your original dict1:

dout = [[dict2[i], [dict2[k] for k in setvalue]]
for i,setvalue in dict1.iteritems()]

- Paddy

Jun 11 '07 #7
anush shetty wrote:
On Jun 12, 2:10 am, James Stroud <jstr...@mbi.ucla.eduwrote:
>>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

So there is no way I can represent all the duplicates right.

So is there any solution where I could take the values of dictionary
and represent them separately?

-
Anush
Just leave off the sum and turn the generator into a list comprehension:

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

James
Jun 11 '07 #8
>
Working from your original dict1:

dout = [[dict2[i], [dict2[k] for k in setvalue]]
for i,setvalue in dict1.iteritems()]

- Paddy

I dont think this works.

Jun 11 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by none | last post: by
4 posts views Thread by brianobush | last post: by
1 post views Thread by Lothar Armbr?ster | last post: by
7 posts views Thread by homecurr | last post: by
3 posts views Thread by Robert L. Smith, Jr. | last post: by
1 post views Thread by john wright | last post: by
8 posts views Thread by Brian L. Troutwine | last post: by
3 posts views Thread by JamesB | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.