Dict--Dictionary
A set of key sets that contains the corresponding key to value.
Python's built-in dictionary, which becomes a map in other languages, uses key-value storage with a few blocks of lookup speed. Compared with list, Dict features:
The dict is extremely fast to find and insert, and does not slow down as the key increases.
Dict requires a lot of memory and a lot of wasted memory.
The lookup and insertion time of the list increases as the element increases.
List takes up little space and consumes little memory.
Dict is a way of exchanging memory space for running time
Dict can be used in places where high-speed lookups are needed, almost everywhere. The Dict key must be an immutable object because Dict calculates the storage location of value based on key, and if the key changes, the value is stored in a different location, and the dict inside is chaotic. The algorithm of calculating position by key becomes hash.
To ensure the correctness of the hash, the object as a key can not be changed. strings, integers, and so on are immutable objects, and list is mutable, so list cannot be a key. A brief introduction to Dict:
Results table
You can use two lists to complete, such as:
names = [' Alias ', ' Billy ', ' David ']
scores = [90,60,75]
Given the name, the result is obtained by taking the element in the position of the list, and then the corresponding position in the other list. The longer the list, the more time it takes.
Use Dict to implement, such as:
result = {' Alias ': $, ' Billy ': $, ' David ', 75}
result[' Alias ']
In this way, the key and value are one by one, and when the data is stored, the value must be stored according to the key, so that the value is obtained directly from the key. A key can only correspond to one value, and if there are two keys in the same dict (one key appears two times), the value of the key that appears will overwrite the value of the same key that occurred earlier.
# #dict中存放key的顺序与录入顺序无关.
Depositing data into Dict
1. Initialize into
result = {' A ': +, ' B ': +, ' C ': 40}
2. Insert by key
result[' abc '] = 12
# #如果key不存在, an error will be encountered when calling. 1. Use in to determine if the key exists.
' ABCD ' in result
2. Get the value corresponding to key via get (), or none if key does not exist
You can return none if key does not exist, or specify the value returned for a nonexistent condition.
Such as:
CO = {' Alias ': 1, ' Billy ': 2, ' Cyndi ': 3}
Co.get (' David ')
Co.get (' David ', 4) removed from Dict
Pop ()
Co.pop (' Alias ')
If you do not specify a key in pop (), an error will be given. Unlike list, the list removes the last element.
Set
Similar to Dict, is also a set of keys, but does not store value. Because key cannot be duplicated, there is no duplicate key in set. Create a set
Creating a set requires providing a list as the input collection.
s = set ([1,2,3,4])
Or
Li = [1,2,3,4]
s = Set (LI)
The order of the elements in the list within a set does not indicate that the set is ordered.
Duplicate elements are automatically filtered. Adding elements to the set
Add ()
S.add (' a ')
List cannot be added as an element to set and will error. Because list is a mutable object. Both set and dict do not support mutable objects as keys.
Remove an element from a set
Remove ()
S.remove (4)
You can remove only one element at a time.
Set can be seen as a set of mathematical meanings, so multiple sets can do the intersection of mathematical meanings and set operations.
>>> S1 = set ([1,2,4,8,10,28])
>>> s2 = set ([2,4,6,8,10,12])
>>> S1 & S2
{8, 2, 4} #交集
>>> S1 | S2
{1, 2, 4, 6, 8, ten,} #并集 immutable objects
First introduce a new function replace ()
A= ' abc '
A.replace (' A ', ' a ')
In the previous example, the types of data such as STR and int are immutable objects, so if you replace them with immutable objects, are they no longer "immutable"? >>> a = ' abc '
>>> a.replace (' A ', ' bbbbbb ')
' BBBBBBBC '
In fact, after replace, print (a), you will find that the value of the object (variable) A is still ABC, that is, object A is actually a variable, but the object A is pointing to the content is the str ' ABC ', after replace is actually a new variable, This behavior can be explained by the following logic: >>> a = ' abc '
>>> Print (a)
Abc
>>> B = a.replace (' A ', ' hhhhh ')
>>> print (b)
Hhhhhbc
>>> Print (a)
Abc
Therefore, for non-mutable objects, any method that invokes an object does not alter the contents of the object itself, just creates a new object and returns it, guaranteeing that the immutable object itself will never be mutable.
Python Learning notes Day4 sict and set