List and Ganso
Demo of list values and slices:
strings = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']print (Strings[0]) # are counted starting from 0, so the first element is 0print (Strings[1:4]) # Slice, starting from the starting point, But does not include the end print (strings[4:]) # Default end point, then cut to the end of print (Strings[:3]) # can also be the default starting point, print from scratch (strings[::2]) # So the head and tail are saved, that is, from start to finish. The step is added here because the 3 parameters of the head and tail step are saved, that is, the entire list print (strings) print (Strings[0:5:2]) # # The third parameter is the step, the default 1print (Strings[-2]) # takes the 2nd print ( Strings[-4:-2] # is always taken from left to right, so-4 put front
Append list, insert list, modify list:
strings = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']strings.append (' z ') # in the last append print (strings) Strings.insert (1, ' I1 ') # Insert to 1 position print (strings) strings[4] = ' D2 ' # Modify print (strings)
To delete a list:
strings = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']strings.remove (' C ') # cannot delete the nonexistent member print (strings) Strings.pop () # Default parameter, Delete the last print (strings) Strings.pop (1) # If the parameter is removed, the deletion of the print (strings) del Strings[0] # is also not possible to delete. This is the same as the pop with the parameter print (strings)
Find elements:
strings = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']strings.append (' E ') print (Strings.count (' E ')) # Returns the number of occurrences of this element in the list print ( Strings.index (' E ')) # Returns the position of this element in the list. But only the first match. However, if you do not find the error print (' F ' in Strings) # in and not have been learned before. Can do whether an element exists in the list of Judgment print (' H ' not in strings)
Invert, merge, sort, and finally empty:
strings = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']print (Strings) Strings.reverse () # Reverse list print (strings) strings.extend ([' 2 ', '! ') , "AA"]) # merge list, directly with the + sign the same, but the + number efficiency problem bar print (strings) Strings.sort () # Sort, the element can not be mixed with numbers and strings, otherwise the sorting will be an error. Here is the 2 character print (strings) # Sort by ASCII code row, symbol, number, uppercase, lowercase. Chinese text Fu last Strings.clear () # Empty list print (strings)
Copy of list using. Copy ()
The elements in the list can also be another listing, as shown below
strings = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']strings.insert (2,[' A ', ' B ', ' C ']) # Insert an element, this element is another list STRINGS2 = Strings.copy () # Copy list print (STRINGS2) strings[0] = ' Apple ' # modifies elements of the original table strings[2][1] = ' Boy ' # 2 The elements in the list are represented by the print (strings) print (STRINGS2) # Note that the 1-layer element does not change, but the elements of the 2-layer are also changed, although the elements of the original table were previously manipulated
Each element in the list occupies a space, the 1-layer element is the value of the space, copy is copied when the value. However, in the case of Layer two, the table in the first layer only occupies 1 elements, and the value stored here is the memory address of the following two-tier tables. So no matter which list modifies the 2-layer element, both tables will change.
The list uses assignment = and the copy effect is different, use = completely copy the memory address, equal to just give the list an alias
strings = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ']STRINGS3 = Stringsprint (STRINGS3) strings[0] = ' Apple ' # Modify the elements of the original table print (strings) Print (STRINGS3) # Although the table was not modified, the content is the same as the original table
So to really copy a table, you need to import copy this module
Import copystrings = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']strings.insert (2,[' A ', ' B ', ' C ']) # Insert an element, this element is another list STRINGS2 = Copy.copy (Strings) # as in the previous copy list STRINGS3 = Copy.deepcopy (strings) # deep Copy, this is the print (STRINGS2) print (STRINGS3) We need Strings[0] = ' Apple ' # Modify the elements of the original table strings[2][1] = ' Boy ' # Modify the elements in Layer 2 print (strings) print (STRINGS2) # 1 layer unchanged, 2 layer or modified print (Strin GS3) # The elements here are completely unaffected by the original table changes
Tuples and lists are similar, but also a set of numbers, it is not once created, it can not be modified, so it is called a read-only list
Tuples have only 2 methods of Count and index.
STRINGS1 = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G '] # This is the list LISTSTRINGS2 = (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ') # This is Ganso Tupleprint (type ( STRINGS1), type (STRINGS2)) print (STRINGS1) print (STRINGS2)
You can store data that you do not want to be modified, such as constants, such as connection parameters, in tuples.
Enumerate () function
It is convenient to get the subscript and value of the element at once, which is more practical
strings = [' A ', ' B ', ' C ', ' d ']# subscript and value constitute a tuple for I in Enumerate (strings): print (i) print (' ****** ') # This writes, the subscript and value are output sequentially, not in the form of tuples There was a i,j in enumerate (strings): print (i,j) print (' ****** ') # Applied with index and item, which makes sense. Alternatively, you can take a parameter that specifies the starting value of the subscript for Index,item in Enumerate (strings,1): Print (Index,item)
String manipulation
The auto-filled function is a bit of a use, just remember. Otherwise, it is very likely to be populated manually when needed.
string = "Hello" Print (String.center (10, ' = ')) # content in the middle, fill print on both sides (String.ljust (10, ' = ')) # content on the left, right fill print (String.rjust ( 10, ' = ') # content on the right, left padding
Parameter 1 defines the length of the string, which is not sufficient to fill with the character of parameter 2.
Dictionary
The dictionary is the data type of Key:value
# Create Dictionary dic1 = {} # Create an empty dictionary Dic2 = {' name ': ' Bob ', ' job ': ' Engineer ', ' gender ': ' Male '}print (DIC1,DIC2)
Modify and add
Dic2 = {' name ': ' Bob ', ' job ': ' Engineer ', ' gender ': ' Male '}dic2[' class ' = ' first ' # If key does not exist, add or subtract dic2[' job ' = ' Teacher ' # If key is present, modify print (DIC2)
There is also an added method, SetDefault, to note that the following parameters are used in the middle of the comma. only new keys are added and no existing keys will be modified.
Dic2 = {' name ': ' Bob ', ' job ': ' Engineer ', ' gender ': ' Male '}dic2.setdefault (' name ', ' Tom ') # Last result, name is not modified Dic2 . SetDefault (' class ', ' first ') # end result, added class this print (DIC2)
Delete: Remove the elements within the dictionary recommendation 2 in the method:
Dic2 = {' name ': ' Bob ', ' job ': ' Engineer ', ' gender ': ' Male '}dic2.pop (' Gender ') del dic2[' job ']print (DIC2)
Find also gave 2 methods, recommended with Get
Dic2 = {' name ': ' Bob ', ' job ': ' Engineer ', ' gender ': ' Male '}print (' job ' in Dic2) # Returns True, returns whether the dictionary has this keyprint (di c2[' job ']) # Returns the value of key (value). If key does not exist then it will error print (Dic2.get (' name ')) # If key exists, return value print (Dic2.get (' name2 ')) # If key does not exist, return none
Can list all keys or all of the value
Dic2 = {' name ': ' Bob ', ' job ': ' Engineer ', ' gender ': ' Male '}print (dic2.values ()) print (Dic2.keys ())
You can also turn the dictionary into a list
Dic2 = {' name ': ' Bob ', ' job ': ' Engineer ', ' gender ': ' Male '}print (Dic2.items ())
Update, merge updated dictionary. Update the tables in the parameters to the previous table. No key will be added, existing key and modified
Dic2 = {' name ': ' Bob ', ' job ': ' Engineer ', ' gender ': ' Male '}dic3 = ' job ': ' Teacher ', ' class ': ' First '}prin T (dic2) print (DIC3) dic2.update (dic3) print (dic2) print (DIC3)
Dictionary initialization: The first parameter is all keys for the dictionary, and the second parameter is the default value for all values. Here the second parameter does not nest a tuple or a list of data types, otherwise the default assigned value will be the memory address of the list or element, causing all value to be the same value associated
Dictmp = {}dic = Dictmp.fromkeys ([' Name ', ' job ', ' gender '], ' n/A ') print (DIC)
Python Automation Development Learning 2