列表类型内置方法
列表内置
1.作用
描述多个值,比如爱好
2.定义方式
hobby_list = ['play','swimming','dancing','666']
lt = list('nick')
print(lt)
3.内置方法
优先掌握
#1. 索引取值
hobby_list = ['play','swimming','dancing','666']
print(hobby_list[-1])
#2. 切片
print(hobby_list[:]) #['play','swimming','dancing','666']
print(hobby_list[::-1]) #从右到左 #['666','dancing','swimming','play']
#3.长度
print(len(hobby_list))
#4. in/not in
hobby_list = ['play','swimming','dancing','666']
print('play' in hoppy_list) #True
#5. for循环
for hobby in hobby_list:
print(hobby)
#6.del删除
del hobby_list[-1]
print(hobby_list)
#7.append() #追加
hobby_list = ['play','swimming','dancing','666']
hobby_list.append('dapao-->piao')
print(hobby_list)
需要掌握
hobby_list = ['play','swimming','dancing','666']
hobby_list.count('666') #对列表内的某一个元素计数 #2
hobby_list.exend([1,2,3,4]) #扩展列表 #Ctrl+鼠标左键
print(hobby_list)
hobby_list.clear() #清除
print(hobby_list) #[]
hobby_list.copy() #复制列表
hobby_list[-1] = '233'
print(hobby_list)
hobby_list.pop() #删除,默认删除最后一个
print(hobby_list)
hobby_list = ['play','swimming','dancing','666','666','666']
print(hobby_list.index('play'))
print(hobby_list.index('play',2,4)) #索引2-4内有没有该值
hobby_list.insert(0,‘1’) #插入
print(hobby_list)
hobby_list.remove('1') #移除
print(hobby_list)
hobby_list.reverse() #反转
print(hobby_list)
hobby_list = [5,7,3,4,2,9,10]
hobby_list.sort() #排序
print(hobby_list) #[2,3,4,5,7,9,10]
4.存一个值还是多个值
多个值
5.有序or无序
有序
6.可变or不可变(重点)
可变
lis = [12,]
print(id(lis))
lis.append('12')
print(id(lis))

浙公网安备 33010602011771号