2018.12.6 python基础学习——列表的魔法(一)
一、列表的功能:
1.支持for循环和while循环:
例如:
test=['lijialun',165,3,'djf','djd'] index=0 while index<len(test): v=test[index] print(v) index+=1 print('===')
也就相当于:
test=['lijialun',165,3,'djf','djd'] for item in test: print(item) print('===')
2.列表中元素的替换与修改:
(1)替换:
#法一:——索引 test=['lijialun',165,3,'djf','djd'] test[2]=[16,222] print(test) #输出结果: #['lijialun',165,16,222,'djf','djd']
#法二:————切片 test=['lijialun',165,3,'djf','djd'] test[1:4]=[77,55,'dddddddd'] print(test) #结果输出: #['lijialun',77,55,'dddddddd','djd']
(2)删除:
#法一:————索引 test=['lijialun',165,3,'djf','djd'] del test[4] print(test) #结果输出: #['lijialun',165,3,'djf']
#法二——切片 test=['lijialun',165,3,'djf','djd'] del test[0:3] print(test) #输出结果: #['djf','djd']
3.支持in操作:
test=[146545,'dnbch',111,222,'sb'] v='sb' in test print(v) #输出结果:Ture
4.可以通过索引,在列表中一直往“里”找寻:
例:
1 test=[146545,'dnbch',111,222,'sb',[1110,'fdgd',666]] 2 v1=test[1][3] 3 print(v1) 4 v2=test[5][1][3] 5 print(v2) 6 7 #输出结果: 8 #c 9 #d