ArticleDirectory
- I. Columns (lists)
- Ii. tuple (Fixed sequence)
- 3. Dictionary)
- Iv. Del description
The following describes several main data structures provided by python.
1. Columns (lists)
I have already introduced the column. Here I will introduce some methods provided by the column.
Append (x): adds a member to the end of the list. You can also use this method to write a [Len (a):] = [X].
Extend (l): accept a new list parameter and add it to the end of the current list. You can also write a [Len (a):] = L.
Insert (I, X): Add a member to a specific position. The first parameter is the index of the location to be added, so. insert (0, x) is added to the front end of the list, while. insert (LEN (A), x) will be added at the end, equal to. append (X ).
Remove (x): remove the first member whose value is equal to X. If the entire list does not have this member, an error is returned ).
Pop ([I]): removes a Member from a list and returns the member to be removed. If no index is input, A. Pop () will return the last member of the list. The same result will be removed from the list.
Index (x): returns the position (INDEX) of the first Member whose value is equal to X. If the entire list does not have this member, an error is returned ).
Count (x): The number of times X appears in the entire list.
Sort (): sorts the members in the list.
Reverse (): reverse the position of members in the entire list.
Using columns, we can implement stacks and queues by using the POP () and append () Methods of columns. If you want to implement queues, you can use Pop () to pass in parameters.
2. tuple (Fixed sequence)
List and string have many in common. They all belong to the special case of sequence data type. Correspondingly, we also have a standard sequence data type: tuple fixed sequence.
Tuple members and members are separated by commas, for example:
T = 1, 2, hello
[Difference between tuple and list]
The tuple method is similar to the list method. It is not described separately. It only introduces methods that do not exist in tuple:
Append (), extend (), remove (), pop (), index ()
In theory, a list can replace tuple. tuple only exists. I understand that:
1) tuple cannot change its members, making its performance faster than list
2) its members cannot be changed, so thatCodeIt is more secure. For example, we can use it to represent (x, y) coordinates, which is clearer and safer than list.
3. Dictionary
The dictionary structure should be a hash table defined by {}, such as dict = {} Or dict = {'A': 0, B ': 1 }. We can see that each dictionary element is a pair of key: value. The Del description introduced in Part 4 can also be used to operate dictionaries. You can try the specific results by yourself. Add an element to the dictionary. You can directly use dict ['C'] = 2.
Dictionary provides the following methods:
Get (Key, default = none): returns the value corresponding to the key. If the key is not in the dictionary, the default value is returned.
Keys (): returns the list of all keys.
Values (): list all values
Clear (): Clear the dictionary
Has_key (key): determines whether the key is in the dictionary. True is returned; otherwise, false is returned.
Items (): returns the list of dict (Key, value) pairs.
Setdefault (Key, default = none): If a key exists in dict, a key is returned. If no, a key is added to dict and the value is default.
Update (dict2): Add another dict element to the current dict, and overwrite it when repeated.
4. Del description
Del can easily remove members from a position in the list, such as a = [1, 2, 3, 4, 5]. After del a [0] is executed, A = [2, 3, 4, 5], and then execute a del a [], then a = [2, 5]. If del A is executed, A is cleared.