列表是一种具备容器功能的类型,其中可以放入任何类型的对象。
长度为n的列表,有效索引值为0到n-1,列表中元素可变,list[i]=x
列表方法
In [3]: print(dir(list))
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
append
list.append(x)在列表末尾添加元素x
clear
list.clear() 清空列表
copy
list_new=list.copy()拷贝列表,会新建一个列表
count
list.count(x)计算x元素在列表中出现的次数
extend
list.extend(x)将元素x或可迭代项x中的元素添加到列表末尾
index
list.index(x)从左到右返回第一个出现的x的索引
insert
list.insert(i,x)在指定的索引i处插入x
pop
list.pop(i)删除指定索引处的值,默认删除最后一个
remove
list.remove(i)从左到右删除第一个出现的指定值i
reverse
list.reverse()将列表翻转倒序排列
sort
list.sort()将列表按递增顺序排列
list.sort(reverse=True)将列表按递减顺序排列