一、列表
作用:多个装备,多个爱好,多门课程,多个女朋友等
定义:[]内可以有多个任意类型的值,逗号分隔
以下是列表的常用操作:
1 l=[1,2,3] #l=list([1,2,3]) 2 # print(type(l)) 3 4 #pat1===》优先掌握部分 5 # 索引:l=[1,2,3,4,5] 6 print(l[0]) 7 # 切片 7 l=['a','b','c','d','e','f'] 8 9 # print(l[1:5]) 10 # print(l[1:5:2]) 11 # print(l[2:5]) 12 # print(l[-1]) 13 14 15 #了解 16 # print(l[-1:-4]) 17 # print(l[-4:]) 18 # l=['a','b','c','d','e','f'] 19 # print(l[-2:]) 20 21 # 追加 22 # hobbies=['play','eat','sleep','study'] 23 # hobbies.append('girls') 24 # print(hobbies) 25 26 # 删除 27 hobbies=['play','eat','sleep','study'] 28 # x=hobbies.pop(1) #不是单纯的删除,是删除并且把删除的元素返回,我们可以用一个变量名去接收该返回值 29 # print(x) 30 # print(hobbies) 31 32 # x=hobbies.pop(0) 33 # print(x) 34 # 35 # x=hobbies.pop(0) 36 # print(x) 37 38 #队列:先进先出 39 queue_l=[] 40 #入队 41 # queue_l.append('first') 42 # queue_l.append('second') 43 # queue_l.append('third') 44 # print(queue_l) 45 #出队 46 # print(queue_l.pop(0)) 47 # print(queue_l.pop(0)) 48 # print(queue_l.pop(0)) 49 50 51 #堆栈:先进后出,后进先出 52 # l=[] 53 # #入栈 54 # l.append('first') 55 # l.append('second') 56 # l.append('third') 57 # #出栈 58 # print(l) 59 # print(l.pop()) 60 # print(l.pop()) 61 # print(l.pop()) 62 63 #了解 64 # del hobbies[1] #单纯的删除 65 # hobbies.remove('eat') #单纯的删除,并且是指定元素去删除 66 67 68 # 长度 69 # hobbies=['play','eat','sleep','study'] 70 # print(len(hobbies)) 71 72 # 包含in 73 # hobbies=['play','eat','sleep','study'] 74 # print('sleep' in hobbies) 75 76 # msg='hello world egon' 77 # print('egon' in msg) 78 79 80 ##pat2===》掌握部分 81 hobbies=['play','eat','sleep','study','eat','eat'] 82 # hobbies.insert(1,'walk') 83 # hobbies.insert(1,['walk1','walk2','walk3']) 84 # print(hobbies) 85 86 # print(hobbies.count('eat')) 87 # print(hobbies) 88 # hobbies.extend(['walk1','walk2','walk3']) 89 # print(hobbies) 90 91 hobbies=['play','eat','sleep','study','eat','eat'] 92 # print(hobbies.index('eat')) 93 94 95 #pat3===》了解部分 96 hobbies=['play','eat','sleep','study','eat','eat'] 97 # hobbies.clear() 98 # print(hobbies) 99 100 # l=hobbies.copy() 101 # print(l) 102 103 # l=[1,2,3,4,5] 104 # l.reverse() 105 # print(l) 106 107 l=[100,9,-2,11,32] 108 l.sort(reverse=True) 109 print(l)
归类 : python相关