1、列表内置方法
>>> for i in dir(list):
... if not i.startswith('_'):
... print(i)
...
append
clear
copy
count
extend
index
insert
pop
remove
reverse
sort
2、列表方法解析
- 初始化一个列表
>>> a = [i for i in range(10)]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- append 在末尾追加对象
>>> a.append(1)
>>> a.append(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: append() takes exactly one argument (2 given)
>>> a.append([1,2])
>>> a.append({'name': 'xiaoming'})
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, [1, 2], {'name': 'xiaoming'}]
- clear 清空当前列表
>>> a.clear()
>>> a
[]
- copy 浅拷贝,返回一个拷贝对象,对于多层嵌套的可变对象还是可变的
>>> c = a.copy()
- count 返回所有匹配结果的数量
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2]
>>> a.count(1)
2
- extend 追加一个可迭代的对象的元素来扩展当前列表
# 生成器
>>> a = [i for i in range(10)]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b = (i for i in range(10, 20))
>>> b
<generator object <genexpr> at 0x000002371427F9A8>
>>> a.extend(b)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
# 将另一个列表扩展到当前列表
>>> a.extend([1,2])
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2]
- index 返回第一个匹配的元素的索引
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2]
>>> a.index(1)
1
- insert 在指定索引位置插入元素
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2]
>>> a.insert(1, 'hello world')
>>> a
[0, 'hello world', 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2]
- pop 移除并返回指定索引的值,默认index=-1,即默认移除最后一个元素
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a.pop()
9
>>> a.pop(0)
0
>>> a.pop()
8
>>> a
[1, 2, 3, 4, 5, 6, 7]
- remove 移除第一个匹配的指定的元素
>>> a
[1, 2, 3, 4, 5, 6, 7]
>>> a.remove(1)
>>> a
[2, 3, 4, 5, 6, 7]
>>> a.remove(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
- reverse 翻转列表,无返回值
>>> a.reverse()
>>> a
[{'name': 'xiaoming'}, [1, 2], 1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
- sort 排序,默认升序,list.sort(*, key=None, reverse=False):key可以指定排序的关键字
# 简单的排序
>>> import random
>>> a = [random.randint(1,1000) for i in range(10)]
>>> a
[434, 190, 170, 650, 694, 659, 77, 724, 29, 530]
>>> a.sort()
>>> a
[29, 77, 170, 190, 434, 530, 650, 659, 694, 724]
>>> a.sort(reverse=True)
>>> a
[724, 694, 659, 650, 530, 434, 190, 170, 77, 29]
# 构建稍微复杂的元素
>>> def gen_random():
... return random.randint(1,1000), random.randint(2000,3000)
...
>>> gen_random()
(845, 2996)
>>>
>>> a = [gen_random() for i in range(10)]
>>> a
[(804, 2904), (931, 2019), (647, 2025), (323, 2947), (494, 2656), (228, 2929), (104, 2917), (431, 2703), (505, 2941), (460, 2335)]
>>>
>>> a.sort(key=lambda arg: arg[1]) # 安装第二个关键字进行排序
>>> a
[(931, 2019), (647, 2025), (460, 2335), (494, 2656), (431, 2703), (804, 2904), (104, 2917), (228, 2929), (505, 2941), (323, 2947)]
>>>
>>> a.sort(key=lambda arg: arg[0]) # 安装第一个关键字进行排序
>>> a
[(104, 2917), (228, 2929), (323, 2947), (431, 2703), (460, 2335), (494, 2656), (505, 2941), (647, 2025), (804, 2904), (931, 2019)]
>>>
3、元组内置方法
>>> for i in dir(tuple):
... if not i.startswith('_'):
... print(i)
...
count
index
4、元组方法解析
元组是不可变的数据类型,只有count和index两个方法,跟列表类似。
- count 获取匹配元素的个数
>>> b
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> b.count(1)
1
>>> b.count(9)
1
- index 返回第一个匹配的元素的值的索引
>>> b
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> b.index(1)
1
>>> b.index(9)
9