列表类型内置方法
列表类型内置方法(list)
一、作用:
用来存放多个值,如多个爱好,多门课程等
二、定义方式
# 第一种方式
hobby_list = ['play', 'swimming', 'dancing'] # 实际存储的是内存地址
# 第二种方式,将字符打散
lt = list('randysun') # 参数放入可迭代对象
结果:
['play', 'swimming', 'dancing']
['r', 'a', 'n', 'd', 'y', 's', 'u', 'n']
三、常用内置方法
主要内置方法
-
索引取值(正向取值+反向取值),即可存也可以取
hobby_list = ['play', 'swimming', 'dancing'] print(hobby_list[-1]) hobby_list[0] = '2233' print(hobby_list)
结果:
dancing
['2233', 'swimming', 'dancing'] -
切片
hobby_list = ['play', 'swimming', 'dancing']
print(hobby_list[::2])
print(hobby_list[::-1]) # 逆序
结果:
['play', 'dancing']
['dancing', 'swimming', 'play']
- 长度len()
hobby_list = ['play', 'swimming', 'dancing']
print(len(hobby_list))
结果
3
- for循环
hobby_list = ['play', 'swimming', 'dancing']
for hooby in hobby_list:
print(hooby)
结果:
play
swimming
dancing
- in / not in(判断值是否存在)
hobby_list = ['play', 'swimming', 'dancing']
print('swimming' in hobby_list)
结果:
True
-
删除del
del hobby_list[-1] # 删除指定值,或删除所有 print(hobby_list)
结果:
['play', 'swimming']
-
追加append()
hobby_list.append('123') print(hobby_list)
结果:
['play', 'swimming', 'dancing', '123']
需要掌握方法
-
统计列表中某一元素出现的次数count(),没有为0
hobby_list = ['play', 'swimming', 'dancing', '1', '1'] print(hobby_list.count('')) print(hobby_list.count('1'))
结果:
0
2 -
扩展列表extends(),+相当于extends
hobby_list = ['play', 'swimming', 'dancing', '1', '1']
hobby_list.extend([1,2,3,4])
print(hobby_list)
结果:
['play', 'swimming', 'dancing', '1', '1', 1, 2, 3, 4]
- 清除clear()
hobby_list = ['play', 'swimming', 'dancing', '1', '1']
print(hobby_list.clear())
结果:
None
- 复制列表copy(),id一样
hobby_list = ['play', 'swimming', 'dancing', '1', '1']
hobby_list2 = hobby_list.copy()
print(hobby_list2)
结果:
['play', 'swimming', 'dancing', '1', '1']
-
删除pop(),默认删除最后一个
hobby_list = ['play', 'swimming', 'dancing', '1', '1'] del_res = hobby_list.pop() # 指定索引删除第几个,将删除的值返回 print(del_res) print(hobby_list)
结果:
['play', 'swimming', 'dancing', '1'] -
索引 index()
hobby_list = ['play', 'swimming', 'dancing', '1', '1']
print(hobby_list.index('play')) # 值不存在则会报错,可以与异常一起连用
结果:
0
- 插入 insert()
hobby_list = ['play', 'swimming', 'dancing', '1', '1']
hobby_list.insert(0, "1") # 指定位置插入值
print(hobby_list)
结果:
['1', 'play', 'swimming', 'dancing', '1', '1']
- 移除remove()根据值删除
hobby_list = ['play', 'swimming', 'dancing', '1', '1']
hobby_list.remove('1') # 只会删除一个
结果:
['play', 'swimming', 'dancing', '1']
-
翻转reverse()
hobby_list = ['play', 'swimming', 'dancing'] hobby_list.reverse() # 使用切片也可以反转 print(hobby_list)
结果:
['dancing', 'swimming', 'play'] -
排序默认升序sort()
``` python
num_list = [4, 2, 7, 4, 44, 432, 1, 35]
num_list.sort()
print(num_list)
```
结果:
[1, 2, 4, 4, 7, 35, 44, 432]
四、存在一个值还是一个值
多个值
五、有序 or 无序
有序
六、可变 or 不可变
可变
lis = [1, 1, 2, 3, 4, 5]
print(lis)
print(id(lis))
lis.append(100)
print(lis)
print(id(lis))
结果:
[1, 1, 2, 3, 4, 5]
2317614604936
[1, 1, 2, 3, 4, 5, 100]
2317614604936
七、总结
7.1 常用操作
- 按索引取值(正向取值+反向取值),即可存也可以取
- 切片
- 长度len
- 成员运算in和not in
- 追加append
- 删除del
- 循环
7.2 常用方法
- insert
- pop
- remove
- count
- index
- clear
- copy
- extend
- reverse
- sort