Thursday, June 14

Learning Python 2: List&Dictionary

THEY ARE MUTABLE. Big difference. So some methods used on them would change the value of themselves instead of creating new objects.

1. List

slice method (a[1:2] = [4,5]) could be treated as two parts: (1). delete the subset referred by the left side; (2). items on the right side are instead into the place where elements are deleted. But this is not the real case, because if it really happen in sequence, a[2:5]=a[3:6] would be invalid.

append method only accepts single object. What this matters is even if you use something like a.append([1,2]), Python will only insert the list as a single object to a instead of add two elements. To achieve the goal of appending a series of elements, one could use concatenation directly. Since List is mutable, methods like append and sort would not create new objects, thus when you call them Python will only return None.

Python builds these data structure to immensely reduce the work people need to do. For example, when we have list, we could implement a stack simply using append and pop method. For a queue, append and sort(0) is enough. Bye bye low level.

Note there are several ways to remove one or more elements in a list: a.remove(value), del a[index], a.pop(index=-1), a[offset_left:offset_right]=[].

2. Dictionary

Dictionary utilize the key/value pair instead of index to manipulate data. Dictionary is used in many many fields where Python could apply to, such as cgi.

Dictionary is unordered, every time you print out a dictionary its items might be in a different order. The book says this is for performance, fast lookup needs keys set in randomized memory. Make sense, like hashing. Anyway, items in dictionary will never have order by nature.

You could explicitly call the key list or value list of a dictionary by a.keys() and a.values(). But if you use it directly in places like a loop, for element in a, it will return its keys one by one.

Dictionary also support pop method, but calling it on a dictionary will delete the key/value pair and return the value.

In order to be the key in dictionary, a variable should be immutable, i.e. the object it refers to has to be types other than list or dictionary. But the value of dictionary could be anything, since it is mutable.

There are two novel usage of dictionary, one is to create dynamic list, the other is to represent sparse data structure. List in Python, though supports dynamically lengthen itself by append method, could not accept append to a place that is 2 or more steps than its end, like for len(a) = 10, you could not assign a[100] = 1. This is could be just like what dictionary is doing. Also, suppose you have sparse matrix, even if it could be done in list, the visualization would be a mess with tons of zeros. However, use dictionary you could just assign a[(2, 4, 5)] = 1, and then assign any other indices to be zero using if...else, All zero elements would not display in this structure, pretty clean.

No comments:

Post a Comment