python基础之列表篇
LIST:
简介:列表是python中最基本的数据结构之一,每个元素都会被分配一个数字也就是索引,亦称为下标。他的第一个元素对应的索引为0,第二个对应的索引为1,以此类推。
一, 列表的声明与赋值
#列表的声明 list1 = ['pisa','heitang','lurou','mianhuatang'] list2 = ['cat','dog','pig'] list3 = ['softdrink','wine','bear']
二,列表的查询
''' 如何读取列表中的元素? ''' list1 = ['pisa','heitang','mianhuatang']
cat1 = list1[0] #pisa cat2 = list1[1] #heitang cat3 = list[2] # mianhuatang cat4 = list[-1] # mianhuatang cat5 = list[-2] #heitang cat6 = list[-3] # pisa
三,列表的切片
''' 列表的切片 ''' list = ['pisa','heitang','mianhuatang','lurou','panghua'] #获取前三只猫 getThreeCat = list[:3]
getThreeCat = list[0:3] #获取第二只猫和第四只猫 heitang,lurou getSecondAndFourthCat = list[1:4:2] #['heitang','lurou'] getSecondAndFourthCat = list[-4:-1:2] #获取最后一只猫 panghua getLastCat = list[-1] #让整个吃货家族反转 getReverseCat = list[::-1]#['panghua', 'lurou', 'mianhuatang', 'heitang', 'pisa']
四,列表添加元素
''' 列表添加元素的三种方法 insert(插入) extend(迭代添加) append(追加) ''' #列表的添加之 append list1 = ['pisa','lurou'] list1.append('mianhuatang') #['pisa','lurou','mianhuatang'] #列表的迭代添加 list1 = ['pisa','mianhuatang','lurou'] list2 = ['heitang','panghua'] list1.extend(list2) # ['pisa', 'mianhuatang', 'lurou', 'heitang', 'panghua'] list2.extend(list1) # ['heitang', 'panghua', 'pisa', 'mianhuatang', 'lurou'] #指定位置添加insert(索引,元素) list1 = ['heitang','pisa','lurou'] list1.insert(1,'panghua') # ['heitang', 'panghua', 'pisa', 'lurou']
五,列表删除元素
''' 列表删除元素 pop,del,remove,clear以及切片的舍弃 ''' #pop() 默认删除最后一个元素,并返回,如果指定索引,则删除对应的值,后面元素的索引自动减一 list = ['pisa','mianhuatang','heitang','lurou','panghua'] cat1 = list.pop() #panghua print(list) #['pisa','panghua','heitang','lurou'] list2 = ['pisa','mianhuatang','heitang','lurou','panghua'] cat2 = list2.pop(2) # heitang print(list2) #['pisa','mianhuatang','lurou','panghua'] #del 是删除整个列表,也就是从内存中抹去这个列表的指针 list3 = ['pisa','mianhuatang','heitang','lurou','panghua'] print(list3) #['pisa','mianhuatang,'heitang','lurou','panghua'] del list3 print(list3) # NameError: name 'list3' is not defined #remove() 是 删除元素,一次也只能删除一个元素,返回值 None list4 = ['pisa','mianhuatang','heitang','lurou','panghua'] list4.remove('pisa') #['mianhuatang', 'heitang', 'lurou', 'panghua'] #clear 清空整个列表 list5 = ['pisa','mianhuatang','heitang','lurou','panghua'] list5.clear() # [] #切片删除相应的元素 list6 = ['pisa','mianhuatang','heitang','lurou','panghua'] list7 = list6[0:2] # 得到['pisa','mianhuatang'] 删除了heitang,lurou和panghua
六,列表的更新
#重新赋值法 list1 = ['a','b','c','d'] list1[1] = 3;#['a',3,'c','d'] #可迭代对象 list2 = ['a','b','c','d'] list2[:2] = '89' #[8,9,'c','d'] list3 = ['a','b','c','d'] list3[:3] = '56' #['5','6','d'] ?为什么是['5','6','d']而不是['5','6','5','d']或者['5','6','c','d']? 首先迭代对象只能循环一次,也是'5'和'6',而切片只是要覆盖'a','b','c',而我们迭代对象只有两位,这样就少了一位,就舍弃掉,变成了['5','6','d']
七,列表的内置函数
#列表的长度 list1 = ['pisa','mianhuatang','panghua','lurou','heitang'] cat_len = len(list1) # 5 #列表的copy 复制一个副本给其他变量 list2 = ['pisa','mianhuatang','panghua','lurou','heitang'] list3 = list2.copy() print(list3) # ['pisa','mianhuatang','panghua','lurou','heitang'] 需要特别的注意python2.x 没有copy方法 #列表的count 统计出现的次数 返回一个Integer list4 = ['a','b','c','c','d'] list4.count('c') # 2 #列表的index 查询L.index(value, [start, [stop]]) 如果没有则报错(ValueError) list5 = ['pisa','mianhuatang','panghua','lurou','heitang'] list5.index('panghua') # 2 list5.index('heierduo') # ValueError #sort L.sort(key=None, reverse=False) 没有返回值 list6 = [1,3,5,3,7,0] list6.sort() # [0, 1, 3, 3, 5, 7] list6.sort(reverse=True) # [7, 5, 3, 3, 1, 0] #reverse 反转 L.reverse() -- reverse *IN PLACE* list7 = ['a','b','c','d'] list7.reverse() # ['d', 'c', 'b', 'a'] # append,clear,extend,insert,pop,remove,上面已经提及过
列表的循环遍历 1.for循环 nameList = ['zhangsan','lisi','wangwu'] for i in nameList: print(i) 结果: zhangsan lisi wangwu for i in nameList: print(nameList.index(i)) 结果: 0 1 2 2.while循环 nameList = ['zhangsan','lisi','wangwu'] len1 = len(nameList) while len1>=0: print(nameList[len1]) len1 -=1 结果: wangwu lisi zhangsan