常见的容器(复合数据类型)
- list 列表
- set 集合
- dict 字典
- tuple 元组
- list:线性表(链表)
ls = [] or list({})
元素的访问:使用下标
遍历:
使用for循环遍历列表
for index in list:
print(index)
使用while循环遍历列表
index=0
while index<len(ls1):
print(ls1[index])
index +=1
常见方法:
append() 尾部追加一个元素
insert() 在对应的位置添加元素,如果index大于等于长度,则添加到最后一个位置
remove() 移除对应的元素
clear() 清空列表
pop() 删除最后一个元素
sort() 排序
reverse() 翻转list的顺序
count() 统计元素的个数
index() 得到元素所对应的下标,如果这个元素不存在,则抛出异常
copy() 复制对象,浅拷贝
extend() 拼接两个列表为一个列表,也就相当于继承了列表