# ----------------------------------------------------- # 创建列表 : [], 用方括号标识列表, 用逗号来分割元素 motorcycles = ['honada','yamaha','suzuki'] print(motorcycles) #['honada', 'yamaha', 'suzuki'] # ----------------------------------------------------- # 修改列表 : 列表[索引] motorcycles[0] = 'ducati' print(motorcycles) #['ducati', 'yamaha', 'suzuki'] motorcycles[1] = "xingfu125" print(motorcycles) #['ducati', 'xingfu125', 'suzuki'] # ----------------------------------------------------- # 列表.append() : 向列表末尾添加元素 motorcycles.append('yamaha') print(motorcycles) #['ducati', 'xingfu125', 'suzuki', 'yamaha'] # ----------------------------------------------------- # 动态创建列表 names = [] names.append("Tom") names.append("Jerry") names.append("John") print(names) #['Tom', 'Jerry', 'John'] # ----------------------------------------------------- # 列表.insert() : 向列表中插入元素(元素可以是异质的) numbers = list(range(0,10)) print(numbers) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] numbers.insert(1,88) print(numbers) #[0, 88, 1, 2, 3, 4, 5, 6, 7, 8, 9] numbers.insert(0,'python:') #插入一个字符串 print(numbers) #['python:', 0, 88, 1, 2, 3, 4, 5, 6, 7, 8, 9] # ----------------------------------------------------- # del 列表[索引] : 删除列表中的某个索引对应的元素 # numbers = ['python:', 0, 88, 1, 2, 3, 4, 5, 6, 7, 8, 9] # 获取列表numbers中"python:"的索引 index = numbers.index("python:") print('index = {0}'.format(index)) # index = 0 del numbers[index] #从列表numbers中删除元素"Python:" print(numbers) # numbers = [0, 88, 1, 2, 3, 4, 5, 6, 7, 8, 9] del numbers[0::2] #从索引0开始以步长2删除列表numbers中的元素 print(numbers) # numbers = [88, 2, 4, 6, 8] # ----------------------------------------------------- # 列表.pop() : 弹出列表中的元素(任何位置) lst = list(range(10,20)) print(lst) # lst = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] # 默认弹出末尾元素 lst_pop = lst.pop() # lst_pop = 19 print(lst) # lst = [10, 11, 12, 13, 14, 15, 16, 17, 18] # 弹出列表中指定索引的元素 lst_pop = lst.pop(0) # lst_pop = 10 print(lst) # lst = [11, 12, 13, 14, 15, 16, 17, 18] lst_pop = lst.pop(4) # lst_pop = 15 print(lst) # lst = [11, 12, 13, 14, 16, 17, 18] # ----------------------------------------------------- # len() : 获取列表的元素个数 lst = [11, 12, 13, 14, 16, 17, 18] num = len(lst) print('num = %d' % num) # num = 7 # ----------------------------------------------------- # 列表.remove() : 根据值删除元素(如果值有多个, 那么只能删除一个) lst = [11, 12, 13, 14, 14, 14, 16, 17, 18] lst_pop = lst.remove(14) print(lst) # [11, 12, 13, 14, 14, 16, 17, 18] print(lst_pop) # None # ----------------------------------------------------- # 列表.sort() : 对列表进行排序 cars = ['BMW', 'AUDI','TOYOTA','BUICK'] cars.sort() print(cars) # ['AUDI', 'BMW', 'BUICK', 'TOYOTA'] 按ASCII码 # 反向排序, 指定sort()参数为: reverse = True cars.sort(reverse = True) print(cars) # ['TOYOTA', 'BUICK', 'BMW', 'AUDI'] # sorted(列表) : 对列表临时排序(既能按特定顺序显示列表元素, 又不改变原始排序) cars = ['BMW', 'AUDI','TOYOTA','BUICK'] cars_sorted = sorted(cars) print(cars_sorted) # ['AUDI', 'BMW', 'BUICK', 'TOYOTA'] print(cars) # ['BMW', 'AUDI', 'TOYOTA', 'BUICK'] # ----------------------------------------------------- # 列表.reverse() : 反转列表 names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu'] names_reverse = names.reverse() print(names) # ['zhaoliu', 'wangwu', 'lisi', 'zhangsan'] print(names_reverse) # None
# ----------------------------------------------------- # 复制列表: [:],创建一个包含整个列表的切片,同时省略起始和终止索引 lst = range(0,10) lst1 = lst # 指向同一个地址, 不是复制 lst2 = lst[:] #复制列表 print('lst : {0} -> {1}'.format(id(lst), lst)) #lst : 31704816 print('lst1 : {0} -> {1}'.format(id(lst1), lst1)) #lst1 : 31704816 print('lst2 : {0} -> {1}'.format(id(lst2), lst2)) #lst2 : 31704864
# ----------------------------------------------------- # 遍历列表 names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu'] for name in names: print('name = {0}'.format(name)) # 'name = zhangsan' # 'name = lisi' # 'name = wangwu' # 'name = zhaoliu'
# ----------------------------------------------------- # 列表解析: 将for循环和创建新元素的代码合并成一行并自动附加新元素 squares = [value**2 for value in range(1,11)] print(squares) # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]