Python permits more than just strings to be used in this manner. Any Python object that is immutable and hashable can be used as a key to a dictionary. In Python any object that can be modified is called mutable. Lists are mutable, because list elements can be added, changed, or removed. Dictionaries are also mutable, for the same reason. Numbers are immutable. If a variable x is holding the number 3, and you assign 4 to x, you've changed the value in x, but you haven't changed the number 3; 3 is still 3. Strings are also immutable. list[n] returns the nth element of list, string[n] returns the nth character of string, and list[n] = value changes the nth element of list, but string[n] = character is illegal in Python and causes an error. Unfortunately, the requirement that keys be immutable and hashable means that lists can't be used as dictionary keys.
But there are many instances when it would be convenient to have a listlike key. For example, it's convenient to store information about a person under a key consisting of both their first and last names, which could be easily done if we could use a two-element list as a key. Python solves this difficulty by providing tuples, which are basically immutable lists-they're created and used similarly to lists, except that when you have them, you can't modify them. But there's one further restriction: keys must also be hashable, which takes things a step further than just immutable. To be hashable, a value must have a hash value (provided by a __hash__ method) that never changes throughout the life of the value. That means that tuples containing mutable values, although they themselves are immutable, aren't hashable. Only tuples that don't contain any mutable objects nested within them are hashable and valid to use as keys for dictionaries. In mathematical terms, a matrix is a two-dimensional grid of numbers, usually written in texts as a grid with square brackets on each side, as shown at right. A fairly standard way to represent such a matrix is by means of a list of lists. But in some applications, such as weather forecasting, it's common for matrices to be very large-thousands of elements to a side, meaning millions of elements in total. It's also common for such matrices to contain many zero elements. In some applications, all but a small percentage of the matrix elements may be set to zero. In order to conserve memory, it's common for such matrices to be stored in a form where only the nonzero elements are actually stored. Such representations are called sparse matrices.
It's simple to implement sparse matrices using dictionaries with tuple indices. Now, you can access an individual matrix element at a given row and column number. A slightly less clear (but more efficient) way of doing this is to use the dictionary get method, which you can tell to return 0 if it can't find a key in the dictionary and otherwise return the value associated with that key. This avoids one of the dictionary lookups. If you're considering doing extensive work with matrices, you may want to look into NumPy, the numeric computation package. The following is an example of how dictionaries can be used as caches, data structures that store results to avoid recalculating those results over and over. A short while ago, I wrote a function called sole, which took three integers as arguments and returned a result. The problem with this function was that it really was time consuming, and because I was calling sole tens of thousands of times, the program ran too slowly. But sole was called with only about 200 different combinations of arguments during any program run. That is, I might call sole(12, 20, 6) some 50 or more times during the execution of my program and similarly for many other combinations of arguments.
By eliminating the recalculation of sole on identical arguments, I'd save a huge amount of time. I used a dictionary with tuples as keys. The rewritten sole function uses a global variable to store previous results. The global variable is a dictionary, and the keys of the dictionary are tuples corresponding to argument combinations that have been given to sole in the past. Then, any time sole passes an argument combination for which a result has already been calculated, it returns that stored result, rather than recalculating it. If you come from a traditional compiled-language background, you may hesitate to use dictionaries, worrying that they're less efficient than lists (arrays). The truth is that the Python dictionary implementation is quite fast. Many of the internal language features rely on dictionaries, and a lot of work has gone into making them efficient. Because all of Python's data structures are heavily optimized, you shouldn't spend much time worrying about which is faster or more efficient. If the problem can be solved more easily and cleanly by using a dictionary than by using a list, do it that way, and consider alternatives only if it's clear that dictionaries are causing an unacceptable slowdown. Dictionaries are a basic and powerful Python data structure, used for many purposes even within Python itself.
The ability to use any immutable object as a key to retrieve a corresponding value makes dictionaries able to handle collections of data with less code and more direct access than many other solutions. We've now surveyed the main data structures in Python, so the next step is to look at the structures Python has to control the flow of a program.
Our website is not responsible for the information contained by this article. Articleinput.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.
Note: This article was sent to us by: Randy C. at 01052010
1. How to make Python script execution in UNIX and Mac OS X and in Windows
All articles are property of their respective authors. Please read our Privacy Policy!
© 2009 ArticleInput.com.