[sage-support] Re: Indexing by elements of a list
Hi dkrumm, On 3 Sep., 09:33, dkrumm dkr...@uga.edu wrote: Is there a data structure in Python that would allow me to do the following: I have a list of positive integer pairs, say [[1,2], [1,3], [2,5]]. For each pair [i,j] in my list I need to store in memory a number c[i,j] Indeed, it is just a Python question. I suggest you read something about Python dictionaries. You could not index a Python dictionary by *lists*, because lists are mutable, and hell breaks if the keys of a dictionary are modified after creation. But they can be indexed by tuples. Hence, you can do: sage: L = [(1,2),(3,4),(5,6)] sage: D = dict(zip(L,range(3))) sage: D {(1, 2): 0, (5, 6): 2, (3, 4): 1} sage: D[3,4] 1 Best regards, Simon -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
[sage-support] Re: Indexing by elements of a list
The dictionary is exactly what I needed. Thanks! On Sep 3, 3:56 am, Simon King simon.k...@uni-jena.de wrote: Hi dkrumm, On 3 Sep., 09:33, dkrumm dkr...@uga.edu wrote: Is there a data structure in Python that would allow me to do the following: I have a list of positive integer pairs, say [[1,2], [1,3], [2,5]]. For each pair [i,j] in my list I need to store in memory a number c[i,j] Indeed, it is just a Python question. I suggest you read something about Python dictionaries. You could not index a Python dictionary by *lists*, because lists are mutable, and hell breaks if the keys of a dictionary are modified after creation. But they can be indexed by tuples. Hence, you can do: sage: L = [(1,2),(3,4),(5,6)] sage: D = dict(zip(L,range(3))) sage: D {(1, 2): 0, (5, 6): 2, (3, 4): 1} sage: D[3,4] 1 Best regards, Simon -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
[sage-support] Re: Indexing by elements of a list
On 9/3/11 2:33 AM, dkrumm wrote: Is there a data structure in Python that would allow me to do the following: I have a list of positive integer pairs, say [[1,2], [1,3], [2,5]]. For each pair [i,j] in my list I need to store in memory a number c[i,j] which is a floating point number with some large precision. I'd like to create some sort of object M indexed by the elements of my list so that I can refer to M[i,j] and obtain the number c[i,j]. One way (in the example above) would be to create a 5x5 matrix M and only use its entries with indices [1,2], [1,3], and [2,5] to store my numbers. That way I can refer to M[2,5], M[1,2], M[1,3] and get my numbers back. However, this seems like a very inefficient solution (my list is going to be very long, so this matrix M would be huge, and only a few of its entries used). Another way to do it is to use a sparse matrix, which does not store the nonzero entries. Just create the matrix and pass the parameter sparse=True. Or, as Simon suggested, use a dictionary. Storage-wise, there's not a huge amount of difference. In fact, IIRC, sparse matrices are implemented using dictionaries in Sage. Thanks, Jason -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org