mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 23:00:18 +00:00
Update docs/writing/style.rst
This commit is contained in:
@@ -14,6 +14,23 @@ A common idiom for creating strings is to use `join <http://docs.python.org/libr
|
|||||||
|
|
||||||
This will set the value of the variable *word* to 'spam'. This idiom can be applied to lists and tuples.
|
This will set the value of the variable *word* to 'spam'. This idiom can be applied to lists and tuples.
|
||||||
|
|
||||||
|
Sometimes we need to search through a collection of things. Let's look at two options: lists and dictionaries.
|
||||||
|
|
||||||
|
Take the following code for example::
|
||||||
|
|
||||||
|
d = {'s': [], 'p': [], 'a': [], 'm': []}
|
||||||
|
l = ['s', 'p', 'a', 'm']
|
||||||
|
|
||||||
|
def lookup_dict(d):
|
||||||
|
return 's' in d
|
||||||
|
|
||||||
|
def lookup_list(l):
|
||||||
|
return 's' in l
|
||||||
|
|
||||||
|
Even though both functions look identical, because *lookup_dict* is utilizing the fact that dictionaries in python are hashtables, the lookup performance between the two is very different.
|
||||||
|
Python will have to go through each item in the list to find a matching case, which is time consuming. By analysing the hash of the dictionary finding keys in the dict can be done very quickly.
|
||||||
|
For more information see this `StackOverflow <http://stackoverflow.com/questions/513882/python-list-vs-dict-for-look-up-table>`_ page.
|
||||||
|
|
||||||
Zen of Python
|
Zen of Python
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user