Container class was once active in the major programming languages, Python also exists in the container, and not as complicated as Java, only two, mainly its list class, that is, variable-length array, equivalent to Java ArrayList, there is a set, the elements inside the unordered, do not allow repetition, Equivalent to HashSet in Java. There is also a dictionary, which is the famous JSON array in JavaScript, the HashMap in Java. The container classes in Java have been mentioned in the "Java" Java Collections class--java in the data structure of the upgraded version (click the Open link), the following is a two small program, described in Python container class.
First, list, collection
A list is a data structure that handles a set of ordered items, that is, you can store a sequence of items in a list. Once you have created a list, you can add, delete, or search for items in the list. Since you can add or delete items, we say that the list is a mutable data type, that is, this type can be changed.
The following procedure illustrates the use of the list in Python, and finally uses the "Java" for ArrayList (click the Open Link) method as early as in Java to weight the ArrayList. Turn the list into a collection, then convert the set into a list. In Python this is not as complicated as Java, because the declaration of any variable does not have to write the variable name. A set is a set of mathematics, disordered, different, and determined.
#-*-coding:utf-8-*-#列表, set arraylist=[1,12,3,4,5,9,7,9,9]; #定义一个数组, also container print "Now ArrayList is:" +str (ArrayList); Print "ArrayList 9 in the number of:" +str (Arraylist.count (9));p rint "1 in ArrayList:" +str (Arraylist.index (1)) + "location"; Arraylist.insert (0, +);p rint "in the No. 0 position of ArrayList Insert 100 ArrayList is:" +str (ArrayList); For I in Range (0,len (ArrayList)): print Arraylist[i],print;arraylist.sort ();p rint "sorted by ArrayList:" +str (ArrayList); Arraylist.reverse ();p rint " The inverted ArrayList is: "+str (ArrayList), Arraylist=set (ArrayList), Arraylist=list (ArrayList);p rint" ArrayList for: "+str ( ArrayList);
The results of the operation are as follows:
The list methods used are summarized as follows:
Insert (I,X): Inserts an item at the specified position. The first argument is which element to insert in front of, with subscript for example, List.insert (0, X) is inserted before the list, List.insert (len (list), x) is equivalent to List.append (x).
Append (x): equivalent to List.insert (Len (a), x)
Index (x): Finds the value in the list X and then returns the subscript of the first element with the value x. There was an error when it was found.
Remove (x): An error occurred while deleting the first element of the value x from the list.
Sort (): Sorts the list elements in situ. Note that this method changes the list instead of returning the sorted list.
Reverse (): Deserializes the list elements. Change the list.
Count (x): Returns the number of times that X appears in the list.
Second, the dictionary
First put the program, you see, if you know what JSON is, then you must know what the dictionary is in Python. Just a change of name.
#-*-coding:utf-8-*-#字典print; hashmap={' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5};p rint ' HashMap The value of the element corresponding to C is: "+str ( Hashmap[' C ']); hashmap[' F ']=6;hashmap.pop (' B ');p rint "HashMap after adding f-6 delete b-2 The value is:" +str (HashMap);
The results of the operation are as follows:
The dictionary in Python is JSON, and its declaration is exactly the same. In fact, Java HashKey A large set of key-value pairs, with key to find value.
A dictionary is a set of unsorted "key value: Values" in which the key values are not identical within the same dictionary. The primary operation for a dictionary is to save a value with a key value, and to find the corresponding value after the given key value. You can also delete a key value by using Del: value pair. If you save a value with an already defined key value, the original vegetation is forgotten. Using a nonexistent key value to find an error.
A dictionary is not a sequence, it is not indexed by a number in a range, it is indexed by a key value, and the key value can be any immutable type. strings and values can always be used as key values. If a tuple contains only strings, numbers, or tuples, tuples can also be used as key values because tuples are immutable. The list cannot be used as a key value because the list can change values in place with its append () method.
The keys () method of the Dictionary object returns a list of all the key values in the dictionary, in a random order. The sort () method is used whenever sorting is required for the returned list of key values. In order to check whether a key value is in the dictionary, use the dictionary's Has_key () method.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Python" Container class