Você está na página 1de 3

2.7.

Dictionaries Problem Solving with Algorithms and Data Structures


6/11/17, 2:57 PM

2.7. Dictionaries
The second major Python data structure is the dictionary. As you probably recall, dictionaries diff er
f rom lists in that you can access items in a dictionary by a key rather than a position. Later in this book
you w ill see that there are many w ays to implement a dictionary. The thing that is most important to
notice right now is that the get item and set item operations on a dictionary are O(1). Another important
dictionary operation is the contains operation. Checking to see w hether a key is in the dictionary or not
is also O(1). The eff iciency of all dictionary operations is summarized in Table 3. One important side
note on dictionary perf ormance is that the eff iciencies w e provide in the table are f or average
perf ormance. In some rare cases the contains, get item, and set item operations can degenerate into
O(n) perf ormance but w e w ill get into that in a later chapter w hen w e talk about the diff erent w ays
that a dictionary could be implemented.

ope ration Big-O Efficie ncy

copy O(n)

get item O(1)

set item O(1)

delete item O(1)

contains (in) O(1)

iteration O(n)

For our last perf ormance experiment w e w ill compare the perf ormance of the contains operation
betw een lists and dictionaries. In the process w e w ill conf irm that the contains operator f or lists is
O(n) and the contains operator f or dictionaries is O(1). The experiment w e w ill use to compare the
tw o is simple. Well make a list w ith a range of numbers in it. Then w e w ill pick numbers at random and
check to see if the numbers are in the list. If our perf ormance tables are correct the bigger the list the
longer it should take to determine if any one number is contained in the list.

We w ill repeat the same experiment f or a dictionary that contains numbers as the keys. In this
experiment w e should see that determining w hether or not a number is in the dictionary is not only
much f aster, but the time it takes to check should remain constant even as the dictionary grow s larger.

Listing 6 implements this comparison. Notice that w e are perf orming exactly the same operation,
number in container . The diff erence is that on line 7 x is a list, and on line 9 x is a dictionary.

Lis ting 6

1 of 3
2.7. Dictionaries Problem Solving with Algorithms and Data Structures
6/11/17, 2:57 PM

1 import timeit
2 import random
3
4 for i in range(10000,1000001,20000):
5 t = timeit.Timer("random.randrange(%d) in x"%i,
6 "from __main__ import random,x")
7 x = list(range(i))
8 lst_time = t.timeit(number=1000)
9 x = {j:None for j in range(i)}
10 d_time = t.timeit(number=1000)
11 print("%d,%10.3f,%10.3f" % (i, lst_time, d_time))

Figure 4 summarizes the results of running Listing 6. You can see that the dictionary is consistently
f aster. For the smallest list size of 10,000 elements a dictionary is 89.4 times f aster than a list. For the
largest list size of 990,000 elements the dictionary is 11,603 times f aster! You can also see that the time
it takes f or the contains operator on the list grow s linearly w ith the size of the list. This verif ies the
assertion that the contains operator on a list is O(n) . It can also be seen that the time f or the contains
operator on a dictionary is constant even as the dictionary size grow s. In f act f or a dictionary size of
10,000 the contains operation took 0.004 milliseconds and f or the dictionary size of 990,000 it also took
0.004 milliseconds.

2 of 3
2.7. Dictionaries Problem Solving with Algorithms and Data Structures
6/11/17, 2:57 PM

Since Python is an evolving language, there are alw ays changes going on behind the scenes. The
latest inf ormation on the perf ormance of Python data structures can be f ound on the Python w ebsite.
As of this w riting the Python w iki has a nice time complexity page that can be f ound at the Time
Complexity Wiki (http://w iki.python.org/moin/TimeComplexity).

Se lf Che ck

Q-4: Which of the list operations show n below is not O(1)?


(A) list.pop(0)

(B) list.pop()

(C) list.append()

(D) list[10]

(E) all of the above are O(1)

Check Me

Q-5: Which of the dictionary operations show n below is O(1)?


(A) 'x' in mydict

(B) del mydict['x']

(C) mydict['x'] == 10

(D) mydict['x'] = mydict['x'] + 1

(E) all of the above are O(1)

Check Me

3 of 3

Você também pode gostar