列表数据类型内置方法
1.方法
描述多个值,比如爱好
2.定义方法
hobby_list = ['play','swimming','dancing','6666']
lt = list['nick']
print(lt)
3.内置方法
优先掌握
- 索引取值
hobby_list = ['play','swimming','dancing','666']
print(hobby_list[-1])
hobby_list[-1] = '2333' #索引修改
print(1,hobby_list[-1])
- 切片
print(hobby_list[:]) #全部取出
print(hobby_list[::-1]) #-1代表从右到左输出
-
长度
print(len(hobby_list)) hobby_list = ['play','swimming','dancing',666]
-
in / not in
hobby_list = ['play','swimming','dancing','666']
print('swimming' in hobby_list) # True
- for 循环
for hooby in hobby_list:
print(hobby)
- del 删除
del hobby_list[-1]
print(hobby_list)
- append( ) # 追加
hobby_list = ['play','swimming','dancing','666']
hobby_list.append('23333')
print(hobby_list)
需要掌握
hobby_list = ['play','swimming','dancing','666']
print(hobby_list.count('666')) #count() 计数
print(hobby_list.extend([1,2,3,4])) # 扩展列表 # Ctrl + 鼠标左键
print(hobby_list.clear()) # 清除
hobby_list2 = hobby_list.copy() #复制列表
print(hobby_list.pop()) #删除 默认删除最后一个
print(hobby_list.index('play',2,4)) #返回play的下标,或play是否在2~4的切片内
print(hobby_list.insert(0,'1')) #插入
print(hobby_list.remove('1')) #移除
print(hobby_list.reverse()) #反转
hpbby_list3 = [1,25,3,7,9]
print(hobby_list3.sort()) #排序
记住就行:
def func(i):
return i[1]
print(hobby_list.sort())
4.存一个值还是多个值
多个值
5.有序or无序
有序
6.可变or不可变(重点)
可变
标签:play,内置,dancing,数据类型,list,列表,print,hobby,swimming From: https://www.cnblogs.com/hanyingshuo/p/17687460.html