#-----------------------------------------------------# Create a list: [], use square brackets to identify the list, divide the element with commas motorcycles = [' Honada ', ' Yamaha ' , ' Suzuki ']print (motorcycles) #[' Honada ', ' Yamaha ', ' Suzuki ']#---------------------------------------------------- -# Modify list: list [index]motorcycles[0] = ' Ducati ' Print (motorcycles) #[' Ducati ', ' Yamaha ', ' Suzuki ']motorcycles[1] = "xingfu125" Print (motorcycles) #[' Ducati ', ' xingfu125 ', ' Suzuki ']#-----------------------------------------------------# List. Append (): add element to the end of the list motorcycles.append (' Yamaha ') print (motorcycles) #[' Ducati ', ' xingfu125 ', ' Suzuki ', ' Yamaha ']#- ----------------------------------------------------# dynamic Create List names = []names.append ("Tom") names.append ("Jerry") Names.append ("John") print (names) #[' Tom ', ' Jerry ', ' John ']#-----------------------------------------------------# List. Insert (): Inserts an element into the list (the element can be heterogeneous) numbers = List (range (0,10)) print (numbers) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]numbers.insert (1 , #[0) print (numbers), 1, 2, 3, 4, 5, 6, 7, 8, 9]numbers.insert(0, ' python: ') #插入一个字符串print (numbers) #[' python: ', 0,, 1, 2, 3, 4, 5, 6, 7, 8, 9]#------------------------------------ -----------------# del list [index]: Delete the element corresponding to an index in the list # numbers = [' python: ', 0, 88, 1, 2, 3, 4, 5, 6, 7, 8, 9]# get list numbers in "PYT Hon: "index = Numbers.index (" python: ") print (' index = {0} '. Format (index)) # index = 0del Numbers[index] #从列表numbers中删 Except for the element "Python:" Print (numbers) # numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]del Numbers[0::2] #从索引0开始以步长2删除列表numbers中的元素 Print (numbers) # numbers = [2, 4, 6, 8]#-----------------------------------------------------# list. Pop (): The element in the pop-up list (any WHERE) LST = List (range (10,20)) print (LST) # LST = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]# default popup end element Lst_pop = Lst.pop () # ls T_pop = 19print (LST) # LST = [10, 11, 12, 13, 14, 15, 16, 17, 18]# the element of the specified index in the pop-up list lst_pop = Lst.pop (0) # Lst_pop = 10prin T (LST) # LST = [One, one, 4, 11, A, a, a, a, a, a, a 18]lst_pop = a. 12 = # Lst_pop = 15print (LST) # lst = [, 13, 14, 18]#-----------------------------------------------------# len (): Gets the number of elements in the list LST = [One, one, one, all, +, +, 18]num = Len (LST) print (' num = %d '% num ' # num = 7#-----------------------------------------------------# list. Remove (): Deletes an element based on value (only one if there are multiple values) LST = [One, one, all, +, +, +, +, 18]lst_pop = Lst.remove (+) print (LST) # [One, A, a, a, a, a, a, a, a, a, a, 18]print (Lst_pop) # none#-----------------------------------------------------# list. Sort (): Sorts the list cars = [' BMW ', ' AUDI ', ' TOYOTA ', ' BUICK ']cars.sort () print (Cars) # [' AUDI ', ' BMW ', ' BUICK ', ' TOYOTA ') by ASCII # reverse sort, specify the sort () parameter to: reverse = truecars.sort (reverse = True) print (Cars) # [' TOYOTA ', ' BUICK ', ' BMW ', ' AUDI ']# sorted (list): Temporarily sort the list (to display list elements in a particular order, without altering the original sort) cars = [' BMW ', ' AUD I ', ' Toyota ', ' BUICK ']cars_sorted = sorted (cars) print (cars_sorted) # [' AUDI ', ' BMW ', ' BUICK ', ' Toyota ']print (Cars) # [' BMW ', ' AUDI ', ' TOYOTA ', ' BUICK ']#-----------------------------------------------------# list. Reverse (): Reverse list name s = [' Zhangsan ', ' Lisi ', ' wanGWU ', ' zhaoliu ']names_reverse = Names.reverse () print (names) # [' Zhaoliu ', ' Wangwu ', ' Lisi ', ' Zhangsan ']print (NA Mes_reverse) # none#-----------------------------------------------------# Copy List names = [' Zhangsan ', ' Lisi ', ' Wangwu ', ' Zhaoliu ']names_copy = Names[:]print (names_copy) # [' Zhaoliu ', ' Wangwu ', ' Lisi ', ' Zhangsan ']#------------------------ -----------------------------# Traversal List names = [' Zhangsan ', ' Lisi ', ' Wangwu ', ' Zhaoliu ']for name in Names:print (' name = {0 } '. Format (name) # ' name = Zhangsan ' # ' name = Lisi ' # ' name = Wangwu ' # ' name = Zhaoliu '
Python Learning notes-list