Python 简明教程

Python - List Methods

列表是 Python 中的基本数据结构之一,它提供了一种灵活的方式来存储和管理一系列项目。它有几个内置方法,允许你高效地添加、更新和删除项目。

List is one of the fundamental data structures in Python, It provides a flexible way to store and manage a collection of items. It has several built-in methods that allow you to add, update, and delete items efficiently.

Python 中的列表可以包含不同类型的数据项,包括其他列表,使其在不同的场景中具有很高的灵活性。list 对象包含几个内置的方法,允许你高效地添加、更新和删除项目,以及对列表元素执行各种操作。

Lists in Python can contain items of different data types, including other lists, making them highly flexible to different scenarios. The list object includes several built-in methods that allow you to add, update, and delete items efficiently, as well as to perform various operations on the list’s elements.

Python List Methods

list 方法使你可以轻松有效地操作列表,无论是追加新项目、删除现有项目,甚至排序和反转列表。通过使用这些内置方法,你可以更有效地在 Python 中使用列表,从而编写更有效率、可读性更高的代码。

The list methods enable you to manipulate lists easily and effectively, whether you are appending new items, removing existing ones, or even sorting and reversing the list. By using these built-in methods, you can work with lists in Python more effectively, allowing you to write more efficient and readable code.

Printing All the List Methods

要查看列表的所有可用方法,可以使用 Python dir() 函数,该函数返回与对象相关的所有属性和功能。此外,可以使用 Python help() 函数获取有关每个方法的更详细信息。例如:

To view all the available methods for lists, you can use the Python dir() function, which returns all the properties and functions related to an object. Additionally, you can use the Python help() function to get more detailed information about each method. For example:

print(dir([]))
print(help([].append))

以上代码片段提供了与列表类相关的所有属性和功能的完整列表。它还演示了如何在 Python 环境中访问特定方法的详细文档。以下是输出结果 −

The above code snippet provides a complete list of properties and functions related to the list class. It also demonstrates how to access detailed documentation for a specific method in your Python environment. Here is the output −

['__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']
Help on built-in function append:

append(object, /) method of builtins.list instance
    Append object to the end of the list.
(END)

下面是 Python 中列表的内置方法,根据其功能进行分类。让我们探索并了解每种方法的基本功能。

Below, the built-in methods for lists in Python, which are categorized based on their functionality. Let’s explore and understand the basic fuctionality of each method.

Methods to Add Elements to a List

以下是专门设计用于向列表中添加新项目/项目的函数 −

The following are the methods specifically designed for adding new item/items into a list −

Sr.No.

Methods with Description

1

list.append(obj) Appends object obj to list.

2

list.extend(seq) Appends the contents of seq to list

3

list.insert(index, obj) Inserts object obj into list at offset index

Methods to Remove Elements from a List

以下是专门设计用于从列表中删除项目的函数 −

The following are the methods specifically designed for removing items from a list −

Sr.No.

Methods with Description

1

list.clear() Clears all the contents of the list.

2

list.pop(obj=list[-1]) Removes and returns the last object or the object at the specified index from the list.

3

list.remove(obj) Removes the first occurrence of object obj from the list.

Methods to Access Elements in a List

用于在列表中查找或计算项的方法如下 −

These are the methods used for finding or counting items in a list −

Sr.No.

Methods with Description

1

list.index(obj) Returns the lowest index in list that obj appears

2

list.count(obj) Returns count of how many times obj occurs in the list.

Copying and Ordering Methods

以下方法用于创建副本并在列表中排列项 −

These are the methods used for creating copies and arranging items in a list −

Sr.No.

Methods with Description

1

list.copy() Returns a copy of the list object.

2

list.sort([func]) Sorts the objects in the list in place, using a comparison function if provided.

3

list.reverse() Reverses the order of objects in the list in place.