前言
列表是一种python类似数组的数据结构,操作灵活,可存储多种类型的数据。
目录
1.列表的基础知识
2.列表的增删改查并操作
3.列表的其他操作
4.基本方法表
(一)列表的基础知识
1.说明:
列表(list),可变序列,可以存储多种数据的集合,用括号 [] 表示列表的开始与结束。
2.定义的基本形式
(1)数值类型

(2)字符串类型

(3)综合类型

(二)列表的增删改查并操作
1.增加元素
(1)append()方法
功能:拓展添加 ,在列表的末尾添加新的元素。
用法:list.append("新元素")

(2)insert()方法
功能:插入添加 在列表的某个位置添加新的元素。
用法: list.insert(index, "新元素")

2.删除元素
(1)clear()方法
功能:清空列表的所有元素,使得原来的列表变为空列表。
用法:list.clear()

(2)pop()方法
功能:删除某个元素,并返回删除的元素
用法:list.pop(index) index 为空时,删除的是末尾的元素。

(3)remove()方法
功能:根据元素来删除来删除元素,而不是根据元素的索引。
用法:list.remove(value)

(4)del()方法
功能:删除列表的部分元素,或者删除整个列表
用法:del(list[index1, index2]) 删除部分元素 del list 删除整个列表

3.修改元素
功能:对于部分元素修改
用法:直接进行赋值来修改

4.查找元素:
(1)index()方法
功能:在列表里查找某元素的索引
用法:list.index(valuse, [start], [end]) ,没有范围时,在全列表搜索,只有start时,从start开始查找。

(1)in 方法
功能:判断某元素是否在列表里,放回布尔值。
用法:“value” in list

5.合并列表
(1) + 号合并
功能:使两个列表合并为同一个列表。
用法: list3 = list1 + list2

(2)extend()方法
功能:在一个列表后面添加另一个列表
用法:list1.extend(list2)

(三)列表的其他操作
1.列表的排序
(1)sort()方法:直接改变原列表
功能:对于列表进行排序
用法:list.sort(key=None, reverse=False) ,, key 是用于修改排列的规则。
注意:数字排数字的,字符串排字符串的,不可混用。
a.默认排序--升序(按照Ascii码排序 )

b.修改reverse值--降序(改为True)

(2)sorted()方法:不改变原列表
功能:对列表进行排序
方法:sorted(iterable,key=None, reverse=False) 与sort 类似

2.对列表的复制
功能:复制列表给一个新的列表
用法:list1 = list.copy(), 与赋值不同的是,直接复制给一个新的变量, 而赋值的指针还是指向原列表。

3.统计某个元素的次数
count()方法
功能:统计列表某个元素的个数
用法:list.count(value)

4.反向记录:
reverse()方法
功能:使列表的元素顺序倒置
用法:list.reverse()

网友评论