This article gives you a summary of the Python 5 built-in data structure and operation examples, very detailed, the need for small partners can refer to.
One, List
A list is a data structure in which a series of items can be stored. List items are separated by commas and enclose all items in a pair of brackets to indicate that this is a list. The following example shows some basic operations of the list:
# define a list Object class_list:class_list = [' Michael ', ' Bob ', ' Tracy ']# get a class_list length print ' class has ', Len (class_list) , ' Students ' # accesses objects in class_list print ' The 3rd student in class ', class_list[2]# inserts objects into Class_list class_list.append (' Pau L ') # Delete an item from Class_list del class_list[0]# sort the class_list class_list.sort () # traverse the entire Class_list project print ' These students a Re: ', for student in Class_list: print student,
The output is:
Class has 3 students
The 3rd student in class is Tracy
These students are:bob Paul Tracy
There are a few things to note about the above code:
You can add any type of object to Class_list, that is, the items in a list are not required to have the same type. You can even insert a list into the class_list.
The sort function acts on itself instead of returning a copy, which is different from the string type because the string is not modifiable.
The end keyword parameter of the print function is used to specify the output after the input is completed, by default, the line break, and the code above replaces the line break with a space character.
Two, tuple (tuple)
The tuple is not much different from the list in terms of usage and concept, and a tuple can be viewed as a read-only list. This means that a tuple cannot be modified once it is defined-it cannot add and delete objects, nor can it modify objects in a tuple.
The items in a tuple should also be separated by commas and enclosed in parentheses to enclose the items as if the table is a tuple. This parenthesis is optional, meaning that a tuple can be defined in the following two ways:
t = ' Adam ', ' Lisa ', ' Bart '
t = (' Adam ', ' Lisa ', ' Bart ')
But it's not necessarily a good habit to omit the parentheses. In addition, when a tuple has only one item, the first item must have a comma after it, in which case the T = (' Adam ',) should be defined. This seems to be an odd constraint, but without the comma, the tuple defined without parentheses becomes the T = ' Adam ', which is clearly ambiguous.
Three, dictionary (Dictionary)
A dictionary can be seen as a set of key-value (key-value) pairs. The key must be unique, and each key is associated with a value. Key must be an immutable object (for example: tuple, numeric, string). Also note that the key-value pairs in the dictionary are not sorted in any way.
The definition of a dictionary should be in such a format d={key1:value1, Key2:value2, key3:value3}. The keys and values are separated by a colon, and the key-value pairs are delimited by commas, followed by curly braces to enclose all key-value pairs. Some basic operations are as follows:
# dictionary Definition d = { ' adam ': 59}#, ' Lisa ': $, ' Bart ': ' $ ' s ' to get the value print ' Adam's score is ', d[' Adam ']# delete a key value to Del d[' Bar T ']# traversal dictionary for name, score in D.items (): print ' {0} is {1} '. Format (name, score) # Add a key value to the dictionary d[' Paul ' = 72# determine if a key exists in the dictionary. can also be used if Ab.has_key (' Lisa ') if ' Lisa ' in D: print "Lisa's address is", d[' Lisa '
The result of the output is:
Adam's score is 95Lisa was 85Adam is 95Lisa's address is 85
Iv. sequence (sequences)
The three built-in data structures described above are sequences, and index operations are a basic operation of a sequence. The subscript operation provides direct access to the objects in the sequence. Above, although the subscript operation has been demonstrated-the queue and tuple with a digital subscript, the dictionary with the keyword subscript.
Sequence subscript is starting from 0, the above example only uses the subscript as a positive number, in fact, the subscript can also be negative, such as -1,-2,-3 .... A negative subscript indicates a position in the opposite direction, such as Class_list[-1], which returns the penultimate item of Class_list.
The sequence not only supports a negative subscript but also supports double subscript, which represents an interval for a double subscript. such as Class_list[0:3] returns a copy of a class_list in a sub-sequence from subscript 1 to subscript 3. Note that this interval is a pair of half-closed half-open intervals. This operation is called a slice operation (slicing operation). If the second subscript of a slice operation exceeds the range of the sequence, the slice operation terminates at the end of the sequence. The two subscripts in the slice operation have default values, the first default is 0, and the second size is the length of the sequence.
You can also provide a third parameter for the slice operation, and the third parameter represents the step size of the slice operation, and its default value is 1. The step length represents the space between the item and the item, such as Name[0:10:3], which returns a sub-sequence of name, labeled 0,3,6,9.
V. Collection (SET)
Collections are aggregates of unordered simple objects. It is appropriate to use a collection when you are only interested in whether an object exists in the aggregation, regardless of the order in which it exists or the number of occurrences. Basic functionality: Determines whether a member of a collection, a collection is a subset of another collection, gets the intersection of two collections, and so on. Instance:
s = set ([' Adam ', ' Lisa ', ' Bart ', ' Paul ']) # to determine if the object is in the collection if ' Bart ' in S: print "Bart was in?", ' Bart ' in s# using the copy function to copy a s ETSC = S.copy () # Add object to Collection Sc.add (' Bill ') # Remove object from collection Sc.remove (' Adam ') # Find the intersection of two sets, or use the S.intersection (SC) print S & SC
Results of the output:
Bart is in? TrueSet ([' Lisa ', ' Paul ', ' Bart '])
Related recommendations:
Fully parse the scope of variables in Python starting with local variables and global variables