Python列表的方法
Python的列表对象是Python提供的最通用的序列。
列表是序列的一种,支持序列的所有操作,像索引、切片、重复等。
查看列表的所有方法:
list = [1, 2, 3]
print(dir(list))
['__add__', '__class__', '__class_getitem__', '__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', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
append
: 在列表的结尾添加一个对象。
append(object, /) method of builtins.list instance
Append object to the end of the list.
clear
: 移除列表中所有的项。
clear() method of builtins.list instance
Remove all items from list.
copy
: 列表的浅复制。
copy() method of builtins.list instance
Return a shallow copy of the list.
count
: 某个值在列表中出现的次数。
count(value, /) method of builtins.list instance
Return number of occurrences of value.
extend
: 在列表的末位添加多个对象(用可迭代的对象扩展原来的列表、用新列表扩展原来的列表)。
extend(iterable, /) method of builtins.list instance
Extend list by appending elements from the iterable.
index
: 某个值在列表中的第一个索引值。没有找到会抛出异常。
index(value, start=0, stop=9223372036854775807, /) method of builtins.list instance
Return first index of value.
Raises ValueError if the value is not present.
insert
: 在列表指定索引的前面插入值。
insert(index, object, /) method of builtins.list instance
Insert object before index.
pop
: 在指定索引(默认-1)删除项并返回被删除的项。列表为空或索引超出范围会抛出异常。
pop(index=-1, /) method of builtins.list instance
Remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
remove
: 删除第一个出现的指定的值。如果值不存在就抛出异常。
remove(value, /) method of builtins.list instance
Remove first occurrence of value.
Raises ValueError if the value is not present.
reverse
: 反转列表。
reverse() method of builtins.list instance
Reverse *IN PLACE*.
sort
: 对列表排序。
sort(*, key=None, reverse=False) method of builtins.list instance
Sort the list in ascending order and return None.
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
order of two equal elements is maintained).
If a key function is given, apply it once to each list item and sort them,
ascending or descending, according to their function values.
The reverse flag can be set to sort in descending order.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现