一、列表类型内置方法(list)
1. 用途:
描述多个值,比如爱好
2. 定义方式:
[]内可以有多个任意类型的值,逗号分隔元素
3. 内置方法:
3.1 优先掌握
- 按索引取值(正向取值+反向取值),即可存也可以取
- 切片
- 长度len
- 成员运算in和not in
- 追加append
- 删除del
- 循环
1.按索引取值(正向取值+反向取值),即可存也可以取
print(f"{'索引取值':-^100}")
hobby_list = ['play', 'swimming', 'dancing', '666']
print(1, hobby_list[1])
hobby_list[-1] = '233' # 索引修改
print(2, hobby_list[-1])
------------------------------------------------索引取值------------------------------------------------
1 swimming
2 233
2.切片
print(f"{'切片':-^100}")
hobby_list = ['play', 'swimming', 'dancing', '666']
print(hobby_list[:]) # ['play', 'swimming', 'dancing', '666']
print(hobby_list[::-1]) # 从右到左 # ['666', 'dancing', 'swimming', 'play']
-------------------------------------------------切片-------------------------------------------------
['play', 'swimming', 'dancing', '666']
['666', 'dancing', 'swimming', 'play']
3.长度
print(f"{'长度':-^100}")
hobby_list = ['play', 'swimming', 'dancing', '666']
print(len(hobby_list))
-------------------------------------------------长度-------------------------------------------------
4
4.成员运算in和not in
print(f"{'in/not in':-^100}")
hobby_list = ['play', 'swimming', 'dancing', '666']
print('play' in hobby_list) # True
---------------------------------------------in/not in----------------------------------------------
True
5.append 追加值
print(f"{'append()追加':-^100}")
hobby_list = ['play', 'swimming', 'dancing', '666']
hobby_list.append('dapao-->piao')
print(hobby_list)
---------------------------------------------append()追加---------------------------------------------
['play', 'swimming', 'dancing', '666', 'dapao-->piao']
6.删除
print(f"{'del删除':-^100}")
hobby_list = ['play', 'swimming', 'dancing', '666']
del hobby_list[-1]
print(hobby_list)
-----------------------------------------------del删除------------------------------------------------
['play', 'swimming', 'dancing']
7.for循环
print(f"{'for 循环':-^100}")
hobby_list = ['play', 'swimming', 'dancing', '666']
for hobby in hobby_list:
print(hobby)
print()
lt = [[1, 2, 3], [2, 3, 4], [5, 6, 7]] # 使用解压缩方式循环
for a, b, c in lt:
print(a, b, c)
play
swimming
dancing
666
1 2 3
2 3 4
5 6 7
3.2 需要掌握
- insert 插入
- pop 删除(按索引)
- remove 移除(按值)
- count 计数
- index 索引
- clear 清空
- copy 拷贝
- extend 扩展列表
- reverse 反转列表
- sort 排序
1.insert() 插入
print(f"{'insert插入':-^100}")
hobby_list = ['play', 'swimming', 'dancing', '666']
hobby_list.insert(0, '1') # 插入
print(hobby_list)
2.pop 删除(按索引)
print(f"{'pop 删除(按索引)':-^100}")
hobby_list = ['play', 'swimming', 'dancing', '666']
hobby_list.pop() # 删除,默认删除最后一个
print(hobby_list)
3.remove 移除(按值)
print(f"{'remove 移除(按值)':-^100}")
hobby_list = ['play', 'swimming', 'dancing', '666']
hobby_list.remove('666') # 移除
print(hobby_list)
4.count 计数
print(f"{'count 计数':-^100}")
hobby_list = ['play', 'swimming', 'dancing', '666', 666, '666']
print(hobby_list.count('666')) # 对列表内的某一个元素计数)
5.index 索引
print(f"{'index 索引':-^100}")
hobby_list = ['play', 'swimming', 'dancing', 'play', '666', 666, '666']
print(hobby_list.index('play')) # 索引
print(hobby_list.index('play', 2, 4)) # 索引2-4内有没有该值
6.clear 清空
print(f"{'clear 清空':-^100}")
hobby_list.clear() # 清除
print(hobby_list)
7.copy 拷贝
print(f"{'copy 拷贝':-^100}")
hobby_list2 = hobby_list.copy() # 复制列表
print(hobby_list2)
8.extend 扩展列表
print(f"{'extend 扩展列表':-^100}")
hobby_list.extend([1, 2, 3, 4]) # 扩展列表 # ctrl+鼠标左键
print(hobby_list)
9.reverse 反转列表
print(f"{'reverse 反转列表':-^100}")
hobby_list.reverse() # 反转
print(hobby_list)
10.sort排序
print(f"{'sort排序':-^100}")
hobby_list.sort() #排序
print(hobby_list)
4. 存一个值还是多个值:
多个值
5. 有序or无序:
有序
6. 可变 or 不可变(重点 可变:值变id不变,不可哈希| 不可变:值变id也变,可哈希):
可变