ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 22 Mar 2012 06:51:48 -0500how to construct a dictionary of dictionaries from a listhttp://ask.sagemath.org/question/8813/how-to-construct-a-dictionary-of-dictionaries-from-a-list/Given the matrix
[0,1,2]
[1,0,3]
[1,2,4]
[0,2,3]
[2,4,1]
[2,3,2]
how to obtain the dictionary
D={0:{1:2,2:3},1:{0:3,2:4},2:{3:2,4:1}} ?Thu, 22 Mar 2012 00:16:14 -0500http://ask.sagemath.org/question/8813/how-to-construct-a-dictionary-of-dictionaries-from-a-list/Comment by canisvetus for <p>Given the matrix
[0,1,2]
[1,0,3]
[1,2,4]
[0,2,3]
[2,4,1]
[2,3,2]
how to obtain the dictionary
D={0:{1:2,2:3},1:{0:3,2:4},2:{3:2,4:1}} ?</p>
http://ask.sagemath.org/question/8813/how-to-construct-a-dictionary-of-dictionaries-from-a-list/?comment=20089#post-id-20089The matrix as a list of rowsThu, 22 Mar 2012 00:18:44 -0500http://ask.sagemath.org/question/8813/how-to-construct-a-dictionary-of-dictionaries-from-a-list/?comment=20089#post-id-20089Answer by DSM for <p>Given the matrix
[0,1,2]
[1,0,3]
[1,2,4]
[0,2,3]
[2,4,1]
[2,3,2]
how to obtain the dictionary
D={0:{1:2,2:3},1:{0:3,2:4},2:{3:2,4:1}} ?</p>
http://ask.sagemath.org/question/8813/how-to-construct-a-dictionary-of-dictionaries-from-a-list/?answer=13386#post-id-13386One simple way would be to loop over the rows:
m = Matrix([[0,1,2], [1,0,3] ,[1,2,4], [0,2,3], [2,4,1], [2,3,2]])
newdict = {}
for a,b,c in m:
if a not in newdict:
newdict[a] = {}
newdict[a][b] = c
sage: newdict
{0: {1: 2, 2: 3}, 1: {0: 3, 2: 4}, 2: {3: 2, 4: 1}}
Those of us who learned Python before we came to Sage will often write this as
from collections import defaultdict
newdict = defaultdict(dict)
for a,b,c in m:
newdict[a][b] = c
instead: a [defaultdict](http://docs.python.org/library/collections.html#collections.defaultdict) is a dictionary which automatically makes a new value (in this case of type "dict") whenever a key that hasn't been seen yet is asked for. It's handy for avoiding a lot of branching to handle uninitialized entries.
Thu, 22 Mar 2012 01:31:18 -0500http://ask.sagemath.org/question/8813/how-to-construct-a-dictionary-of-dictionaries-from-a-list/?answer=13386#post-id-13386Comment by canisvetus for <p>One simple way would be to loop over the rows:</p>
<pre><code>m = Matrix([[0,1,2], [1,0,3] ,[1,2,4], [0,2,3], [2,4,1], [2,3,2]])
newdict = {}
for a,b,c in m:
if a not in newdict:
newdict[a] = {}
newdict[a][b] = c
sage: newdict
{0: {1: 2, 2: 3}, 1: {0: 3, 2: 4}, 2: {3: 2, 4: 1}}
</code></pre>
<p>Those of us who learned Python before we came to Sage will often write this as</p>
<pre><code>from collections import defaultdict
newdict = defaultdict(dict)
for a,b,c in m:
newdict[a][b] = c
</code></pre>
<p>instead: a <a href="http://docs.python.org/library/collections.html#collections.defaultdict">defaultdict</a> is a dictionary which automatically makes a new value (in this case of type "dict") whenever a key that hasn't been seen yet is asked for. It's handy for avoiding a lot of branching to handle uninitialized entries.</p>
http://ask.sagemath.org/question/8813/how-to-construct-a-dictionary-of-dictionaries-from-a-list/?comment=20085#post-id-20085Thank you very much. Sincerely Eusebio CorbachoThu, 22 Mar 2012 06:51:48 -0500http://ask.sagemath.org/question/8813/how-to-construct-a-dictionary-of-dictionaries-from-a-list/?comment=20085#post-id-20085