ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 15 Dec 2010 14:45:48 +0100multidictionarieshttps://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 09:59:52 +0100https://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>
https://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 10:16:19 +0100https://ask.sagemath.org/question/7815/multidictionaries/?answer=11863#post-id-11863Comment 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>
https://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 11:05:50 +0100https://ask.sagemath.org/question/7815/multidictionaries/?comment=22404#post-id-22404Comment 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>
https://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 14:45:48 +0100https://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>
https://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 13:43:38 +0100https://ask.sagemath.org/question/7815/multidictionaries/?comment=22403#post-id-22403