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.Wed, 15 Dec 2010 07:45:48 -0600multidictionarieshttp://ask.sagemath.org/question/7815/multidictionaries/Hello!
I need to store more data, and in order to do so, I would need multi-dimensional version of a dictionary... For example, let m be an object such that if you give the name and the age, such as m [Cathy, 19], then it returns the phone number. (In a dictionary you have one variable, and now I must call the data by more variables...) Do you know such a command? Wed, 15 Dec 2010 02:59:52 -0600http://ask.sagemath.org/question/7815/multidictionaries/Answer by Jason Bandlow for <p>Hello!</p>
<p>I need to store more data, and in order to do so, I would need multi-dimensional version of a dictionary... For example, let m be an object such that if you give the name and the age, such as m [Cathy, 19], then it returns the phone number. (In a dictionary you have one variable, and now I must call the data by more variables...) Do you know such a command? </p>
http://ask.sagemath.org/question/7815/multidictionaries/?answer=11863#post-id-11863If I correctly understand your question, dictionaries can do this. You just need to use tuples as keys.
Example:
sage: m = {} # an empty dictionary
sage: m[('Cathy', 19)] = '555-1212'
sage: m[('Cathy', 19)]
'555-1212'Wed, 15 Dec 2010 03:16:19 -0600http://ask.sagemath.org/question/7815/multidictionaries/?answer=11863#post-id-11863Comment by Mike Hansen for <p>If I correctly understand your question, dictionaries can do this. You just need to use tuples as keys.
Example:</p>
<pre><code>sage: m = {} # an empty dictionary
sage: m[('Cathy', 19)] = '555-1212'
sage: m[('Cathy', 19)]
'555-1212'
</code></pre>
http://ask.sagemath.org/question/7815/multidictionaries/?comment=22402#post-id-22402You can also construct the tuples "implicitly" by doing m['Cathy', 19] = '555-1212' .Wed, 15 Dec 2010 07:45:48 -0600http://ask.sagemath.org/question/7815/multidictionaries/?comment=22402#post-id-22402Comment by Jason Bandlow for <p>If I correctly understand your question, dictionaries can do this. You just need to use tuples as keys.
Example:</p>
<pre><code>sage: m = {} # an empty dictionary
sage: m[('Cathy', 19)] = '555-1212'
sage: m[('Cathy', 19)]
'555-1212'
</code></pre>
http://ask.sagemath.org/question/7815/multidictionaries/?comment=22403#post-id-22403You're welcome! In fact, lists won't work as keys. Look up 'mutable' vs. 'immutable' in connection with Python to understand why.Wed, 15 Dec 2010 06:43:38 -0600http://ask.sagemath.org/question/7815/multidictionaries/?comment=22403#post-id-22403Comment by Katika for <p>If I correctly understand your question, dictionaries can do this. You just need to use tuples as keys.
Example:</p>
<pre><code>sage: m = {} # an empty dictionary
sage: m[('Cathy', 19)] = '555-1212'
sage: m[('Cathy', 19)]
'555-1212'
</code></pre>
http://ask.sagemath.org/question/7815/multidictionaries/?comment=22404#post-id-22404Thank you very much! (I also thought a similar thing but with lists in the argument. But I prefer tuples :-) )Wed, 15 Dec 2010 04:05:50 -0600http://ask.sagemath.org/question/7815/multidictionaries/?comment=22404#post-id-22404