Python 中的列表方法

Python 列表方法有多种方法可用于处理 Python 列表,下面我们将介绍可用于Python列表的所有方法,例如,append()、copy()、insert() 等。

Python 中的列表方法

编号方法描述
1append()用于将元素追加和添加到列表末尾。 
2copy()创建列表的浅拷贝。
3clear()此方法用于从列表中删除所有项目。 
4count()返回值给定元素在列表中的出现次数。
5extend()将可迭代的每个元素添加到列表的末尾
6index()返回第一个值为给定元素的索引。 
7insert()在列表中的给定索引处插入给定元素。 
8pop()删除并返回列表中的最后一个值或给定的索引值。
9remove()从列表中删除给定的对象。 
10reverse()颠倒列表中的元素顺序。
11sort()按升序、降序或用户定义的顺序对列表进行排序。
12min()计算List中所有元素的最小值。
13max()计算List所有元素的最大值。

在列表中添加元素

Python append()

用于向列表追加和添加元素。它用于将元素添加到Python中List的最后一个位置。 

语法: list.append (element)

# 将列表元素添加为列表的值。
List = ['数学', '化学', 1997, 2000]
List.append(20544)
print(List)

输出:

[‘数学’、‘化学’, 1997, 2000, 20544]

Python insert()

在指定位置插入一个元素。 

语法:list.insert(<position, element)

注意:提到的位置应该在List的范围内,在本例中是0到4之间,否则会抛出IndexError。 

List = ['数学', '化学', 1997, 2000]
# 在索引2处插入值10087
List.insert(2, 10087)
print(List)

输出:

['数学', '化学', 10087, 1997, 2000]

Python extend()

将 List2 的内容添加到 List1 的末尾。 

语法: List1.extend(List2)

List1 = [1, 2, 3]
List2 = [2, 3, 4, 5]

# 将List2添加到List1中
List1.extend(List2)
print(List1)

# 现在将List1添加到List2中
List2.extend(List1)
print(List2)

输出:

[1, 2, 3, 2, 3, 4, 5]
[2, 3, 4, 5, 1, 2, 3, 2, 3, 4, 5]

Python List 的重要函数

一些基本的 Python 列表函数以及如何在列表中使用它们。

Python sum()

计算 List 中所有元素的总和。 

语法: sum(列表)

List = [1, 2, 3, 4, 5]
print(sum(List))

输出:

15

Python count()

计算列表中给定元素的总出现次数。 

语法: List.count(元素)

List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.count(1))

输出:

4

Python length

计算List的总长度。 

语法: len(列表名称)

List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(len(List))

输出:

10

Python index()

返回第一次出现的索引。开始和结束索引不是必需的参数。 

语法: List.index(元素[,开始[,结束]])

List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.index(2))

输出:

1

另一个例子: 

List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.index(2, 2))

输出:

4

Python min()

计算 List 的所有元素中的最小值。

语法: min(iterable, *iterables[, key])

numbers = [5, 2, 8, 1, 9]
print(min(numbers))

输出:

1

Python max()

计算 List 中所有元素的最大值。

语法: max(iterable, *iterables[, key])

numbers = [5, 2, 8, 1, 9]
print(max(numbers))

输出:

9

sort()和reverse()函数

Python reverse()

按升序对给定的数据结构(元组和列表)进行排序。Key 和reverse_flag 不是必需的参数,如果没有通过sorted() 传递任何内容,则reverse_flag 将设置为False。 

语法:

sorted([list[,key[,Reverse_Flag]]])

 list.sort([key,[Reverse_flag]])

List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]

# 设置逆序标志为True
List.sort(reverse=True)

# List.sort().reverse(),反转排序后的列表
print(List)

输出:

[5.33, 4.445, 3, 2.5, 2.3, 1.054]

删除列表元素

要删除一个或多个元素,可以使用许多内置函数,例如 pop() 和 remove() 以及 del 等方法。

Python pop()

索引不是必需的参数,如果没有则取最后一个索引。 

语法: list.pop([索引])

注意:索引必须在List的范围内,否则会出现IndexError。 

List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(List.pop())

输出:

2.5
List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(List.pop(0))

输出:

2.3

Python del() 

使用列表名称和索引提及要删除的元素。 

语法:del list.[index]

List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
del List[0]
print(List)

输出:

[4.445, 3, 5.33, 1.054, 2.5]

Python remove()

使用列表名称和元素提及要删除的元素。 

语法:list.remove(element)

List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
List.remove(3)
print(List)

输出:

[2.3、4.445、5.33、1.054、2.5]

原创文章,作者:jkhxw,如若转载,请注明出处:https://www.jkhxw.com/python-list-methods/

(0)
上一篇 2023年9月24日 上午10:21
下一篇 2023年9月24日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注