python basics


Difference between list and tuple

The list is similar to the array in php and can be added, deleted and modified

After the tuple is set, it can only be read and cannot be modified


dict and set 

The query speed of dict is very fast, using key-value pairs to store key=>value 

Dict is the second implementation method. Given a name, for example 'Michael', dict can directly calculate Michaelthe "page number" of the corresponding storage score internally, that is 95, the memory address where this number is stored, and directly retrieve it, so the speed is very fast. (The stored address is first queried in the dict, and then the data is found)

As you can guess, in this key-value storage method, when you put it in, you must calculate the storage location of the value according to the key, so that you can get the value directly according to the key when you take it.


Compared with list, dict has the following characteristics:

  1. The speed of searching and inserting is extremely fast and will not increase with the increase of keys;
  2. It takes up a lot of memory and wastes a lot of memory.

And list is the opposite:

  1. The time to find and insert increases with the number of elements;
  2. Small footprint and little wasted memory.

set (no key in set)

Similar to dict, set is also a collection of keys, but does not store value. Since keys cannot be repeated, there are no repeated keys in the set.

To create a set, provide a list as the input set:

The only difference between set and dict is that the corresponding value is not stored. However, the principle of set is the same as that of dict. Therefore, mutable objects cannot be placed, because it is impossible to judge whether two mutable objects are equal, and there is no guarantee of set. Inside "there will be no repeating elements". Try putting the list into the set and see if an error will be reported.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324790875&siteId=291194637