Python learning day3 ------ list, tuples, pythonday3 ------
I. List
Add brackets [] After the variable name. Next we will introduce how to query the list.
1 #! /Usr/bin/env python 2 #-*-Coding: UTF-8-*-3 # Author: Eric. shen 4 5 test = [] # name of the list 6 7 name = ["zhangsan", "lisi", "wangmazi", "chenwu ", "gousheng"] 8 print (name [0], name [2]) # retrieve data from a known location, because the computer starts from 0 when storing data 9 #> zhangsan wangmazi10 11 # Query 12 # Slice 13 print (name []) # obtain the first digit from the computer to the second digit: Gu Shou regardless of the end, 14 # >>> ['lisi', 'wangmazi'] 15 16 17 print (name [4]) # You can use this method to obtain the last 18 # >>>> gousheng19 20 21 print (name [-1]) of the list when you know the length of the list. # You can use this method to obtain the last 22 # >>>> gousheng23 24 25 print (name [-2]) without knowing the length. # It can also be understood that the number of BITs ranges from 0 to the left: 26 # >>>> chenwu27 28 29 print (name [-2:]) # Even if a negative number is written, it is also the number from left to right, 30 # but there is a mechanism to ignore the tail, 31 # But when I want to take the last digit, it will be ignored, 32 # so you can leave nothing behind the colon at this time. 33 #>>>> ['chenwu', 'gousheng'] 34 35 36 print (name [0: 3]) 37 # >>>> ['hangsan', 'lisi', 'hangmazi'] 38 print (name [: 3]) # The results are the same, if the preceding value is 0, you can omit 39 # >>>> ['hangsan', 'lisi', 'hangmazi']
View Code
Add, replace, or delete a list.
1 #! /Usr/bin/env python 2 #-*-Coding: UTF-8-*-3 # Author: Eric. shen 4 5 test = [] # name of the list 6 7 name = ["zhangsan", "lisi", "wangmazi", "chenwu ", "gousheng"] 8 # Add 9 # append data 10 name. append ("quandan") 11 # >>> ['hangsan', 'lisi', 'hangmazi', 'chenwu', 'gousheng ', 'quandand'] 12 13 # insert it in the specified position 14 name. insert (1, "mingzi") # Write the data into the second machine location 15 # >>> ['hangsan', 'mingzi', 'lisi ', 'wangmazi', 'chenwu', 'gousheng ', 'quandanc'] 16 17 # Change 18 name [2] = "qiangzi" # Replace lisi on machine Location 2 with qiangzi19 # >>> ['zhangsan ', 'mingzi', 'qiangzi', 'wangmazi', 'chenwu', 'gousheng', 'quandan '] 20 21 # Delete 22 names. remove ("wangmazi") # Delete wangmazi23 del name [3] # Delete name 24 name on machine location 3 if you know the name. pop () # Delete the last one without entering the machine location by default. If the input machine location is the same as del name [3], 25 print (name)
View Code
Clear, reverse, sort, and expand
1 #! /Usr/bin/env python 2 #-*-Coding: UTF-8-*-3 # Author: Eric. shen 4 5 test = [] # name of the list 6 7 name = ["zhangsan", "lisi", "wangmazi", "chenwu", "gousheng ", "lisi"] 8 # Find the position of wangmazi 9 print (name. index ("wangmazi") 10 # >>>> 211 # Find the name 12 print (name [name. index ("wangmazi")]) 13 # >>>> wangmazi14 15 # multiple 16 print (name. count ("lisi") # Find the number of 17 18 # names called lisi. clear () # clear list 19 # name. reverse () # reverse list 20 # name. sort () # sort by ascll code 21 name1 = [1, 2, 4] 22 name. extend (name1) # extended. After name1 is added to the name, name1 is not deleted 23 print (name) 24 # >>> ['hangsan', 'lisi ', 'wangmazi', 'chenwu', 'gousheng', 'lisi', 1, 2, 3, 4]
View Code
Copy and deep copy
1 #! /Usr/bin/env python 2 #-*-Coding: UTF-8-*-3 # Author: Eric. shen 4 import copy 5 6 test = [] # name of the list 7 # name [] is the address to which the name of a list is stored in the memory, your change of name2 in name will also become 8 # Because they point to the memory address, change name2, and change name to 9 name = ["zhangsan", "lisi ", "wangmazi", ["eric", "jack"], "chenwu", "gousheng", "lisi"] 10 11 name2 = name. copy () # copying a copy is just a small copy, copy only list 12 name [2] = "" 13 name [3] [0] = "ERIC" 14 print (name, "\ n", name2) 15 16 # This value is 17 name2 = name18 # Deep copy is full copy. You need to add copy mode 19 name2 = copy. deepcopy (name)
View Code
Split
1 #! /Usr/bin/env python 2 #-*-Coding: UTF-8-*-3 # Author: Eric. shen 4 import copy 5 6 test = [] # name of the list 7 # name [] is the address to which the name of a list is stored in the memory, your change of name2 in name will also become 8 # Because they point to the memory address, change name2, and change name to 9 name = ["zhangsan", "lisi ", "wangmazi", ["eric", "jack"], "chenwu", "gousheng", "lisi"] 10 # Skip two slices for 11 print (name [0: -]) # The purpose of this statement is to split two hops from 0 to-1.
View Code
Ii. tuples
Locate data
1 #! /Usr/bin/env python 2 #-*-Coding: UTF-8-*-3 # Author: Eric. shen 4 5 6 # The tuples have two functions: count and index. The machine bit 7 name = ("zhangsan", "lisi", "wangmazi") 8 print (name. count ("zhansgan") 9 # >>>> 010 print (name. index ("lisi") 11 # >>> 1
View Code