Set object
Collection objects in any language are the most important, including fixed arrays, variable arrays, dictionaries (hashtable), and some other special collections ,. net System. collection takes system. collection. specialize class, Java. util. the classes in the collections package are related to this. in addition, the arrays in both environments are fixed-length arrays, but the variable-length list class arraylist is also provided (this name is used in both environments ).
In PHP, arrays and dictionaries are mixed, that is, they can be accessed in sequence, or a key can be set for each element of the array.
So how to use the collection classes in Python?
Lists (List class ):
This class can be analogous to the arraylist in. NET and Java. It is a variable-length array without type constraints;
Mytags = ['A', 'A', 'B', 'A', 'a']. This statement creates a list object and assigns an initial value, note that the initial values here are enclosed by arc [], and there are other collection types behind them. Their symbols are slightly different. Be careful
- Add or delete a modified Element
The following statement adds an integer object to the initialized Array
Mytags. append (0). In this statement, add the integer 0 to the list.
The corresponding method for deleting an element is remove. For example, the following statement deletes an element whose content is B.
Mytags. remove ('B'). Note that if the list contains two elements whose content is B, the Remove Method deletes the element with a smaller serial number, if the list contains "B" elements, an error is returned.
Mytags [0] = "ABC", change the first item of this list to "ABC". Note that the array is still from 0, unlike VB and FORTRAN (I have never heard of it? There is a generation gap...) from 1.
The method for printing this list is very simple.
Print mytags
Output result:
['A', 'A', 'B', 'A', U' \ u554a', 0]
No cycle is required. It's convenient. More powerful functions are coming soon.
We can also sort List objects,
Mytag. Sort ()
The preceding statement execution result is
>>> Print mytags
[0, 'A', 'B', U' \ u554a']
It can be seen that the sorting is based on the corresponding order of the ASCII code table. The Chinese character is converted to Unicode, indicating that the Chinese character is displayed side by side at the end ,. net and Java have this function, and the implementation is different. See the flip list below.
- Reverse list
Using mytags. Reverse (), we can flip the list and print it again. The output result is
[U' \ u554a', '{', 'B', 'A', 'A', '\', 'A', 0]
This method. net arraylist is also provided, and Java uses the reverse static method of the Collection class to operate on a collection object, such as arraylist list = new arraylist (), collection. reverse (list). This class also provides sorting methods to obtain the maximum/small value, copy, and loop. However, this practical class is often ignored!
- Other methods
The List class also has many learning methods, including viewing the length of the list using the Count attribute. Pop pops up the data at the specified position. We will not explain them one by one. If you are interested, you can use the Dir command to view the methods available for the list object.
Tuple (unchanged List)
The name of this class is for. NET and JavaProgramThe clerk is not intuitive and has no corresponding terms, but his concept is actually very simple, that is, once created, it cannot be changed. Although tuple does not have a public method to add elements, it uses some built-in methods, we can still do this.
- creating a tuple
mytuple = ('field', 'method', 'class') is similar. net array declaration and initialization string [] mytags = {". net "," Database "," Python "};
note that the original arc () is used here, not in the middle arc. net declares the Large arc of the array {}
- usage of tuple in the output statement
Print '% s is % d years old' % (name, age ), this statement fills the name and age in order to the top position of the string, and then outputs it. The % s is calculated by the person who has used C language , % d is certainly no stranger. A pin content is a string and a specified content is a number. the following % d can also be written as S %. In fact, you can write % s for all the situations, so that python can handle the conversion work by itself, but writing the correct type can eliminate the following situations, for example, if your program has an error and you accidentally add a string to your age, the error is returned when you run Code here.
. net. writeline ("'{0} is {1} years old", name, age), you can see. net needs to be more flexible, and the name and age do not have to be given in order, such as console. writeline ("'{1} is {0} years old", age, name) has the same effect as the preceding statement