[Original] data structure and Python data structure for getting started with python
Preamble:
C/c ++ has been used for five years before learning python. Although I just learned about python, I also learned the power of python and opened up my own vision and thinking.
One Variable
The variables in python are different from those in c/c ++. In c/c ++, the essence of a variable is the memory address, but in python, when we define a variable and assign a value, the following:
a='ABC'
The python interpreter does two things: (1) create a 'abc' string in the memory; (2) create a variable named a in the memory, and point it to 'abc '. That is to say, python consumes more memory than c/c ++. Here we must correctly process the Defined variables in python.
Binary encoding
There are historical factors in encoding. Since the computer was originally made in the United States, it was the first to encode the commonly used 127 characters into the computer, that is, ASCII code. However, in the future development, we still need to process characters such as Chinese characters. In this case, a single byte is not enough and at least two bytes are required. Unicode was invented to handle all the languages around the world. Unicode usually uses two bytes to represent a single character. For some remote users, four bytes may be used.
Although Unicode is introduced to solve the encoding problem in multiple languages, there is still a waste of resources in use. If Unicode encoding is used in a unified manner, but all texts are in English, isn't it a waste of memory? To solve this problem, UTF-8 encoding was introduced later. In UTF-8 encoding, Unicode characters are allocated according to the number of bytes required by characters. For example, English letters are encoded into 1 byte, and Chinese characters are usually 3 bytes. This saves a lot of memory.
Three lists and tuple
List and tuple are both built-in python data types. The difference is that tuple cannot be changed once it is initialized. Define a list as follows:
#define a listl=['Michael','Luffy','Nancy']
List is an ordered set. Like arrays in c/c ++, subscripts start from 0 and can be added or deleted at any time. You can perform the following operations on the list: append (), insert (), pop (), and [].
Of course, list can be defined by nesting, as follows:
#define a nest listl=['Michael','Luffy','Nancy', ['Corey','Jason']]
Tuple must be assigned a value during initialization, as shown below:
#define a tuplet=('Michael','Luffy','Nancy')
Here we need to explain the Non-modification of tuple. Tuple cannot be modified only by pointing to its elements. For example, in the above example, tuple points to 'Michael ', 'luffy', and 'nancy' respectively ', what cannot be changed is that it can only point to these strings all the time, but cannot point to other strings after definition.
After understanding the true meaning of "unchangeable", we can define a tuple that can change the content. As follows:
#define a alterable tuplet=('Michael','Luffy','Nancy',['Corey','Jason'])t[3][0]='Jefrey't[3][1]='Avery'
From the code above, we can see that a tuple named t is defined first. After the data is modified, the list pointed to during initialization is not modified, but the list content is changed. This is allowed for python.
Dict
Dict in python is the map in c/c ++ and is a structure composed of key/value pairs. For dict, we should pay attention to its key requirements,Dict requires that the key must be an unchangeable object. This is because dict calculates the storage location of Value based on the key. If the result of calculating the same key is different each time, the dict is confused internally, the algorithm that uses this key to calculate the location is called a hash algorithm.
In python, strings and integers are non-variable objects, while lists are variable objects and cannot be used as keys.
Postscript
All of the above are summary of python. If you have any errors, please leave a message !!!