脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python列表的定义及使用

Python列表的定义及使用

2022-02-22 00:02一碗周 Python

这篇文章主要介绍了Python列表的定义及使用,在Python中,列表是由一系列元素按照特定的顺序构成的数据结构,也就是说列表类型的变量可以存储多个数据,且可以重复,下面一起进入文章学习学习内容,需要的朋友可以参考一下

1、定义和使用列表

Python中,列表是由一系列元素按照特定的顺序构成的数据结构,也就是说列表类型的变量可以存储多个数据,且可以重复。

1.1 定义列表

使用[]字面量语法定义变量,列表中的多个元素使用逗号,进行分割,

示例代码如下:

?
1
2
3
4
list1 = ["Hello", "一碗周", "你好"]
list2 = [1, 2, 3, 4, 5]
print(list1)  # ['Hello', '一碗周', '你好']
print(list2)  # [1, 2, 3, 4,5]

使用Python内置的list将其他序列编程列表,示例代码如下:

?
1
2
3
4
list1 = list(range(10))
list2 = list("hello")
print(list1)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list2)  # ['h', 'e', 'l', 'l', 'o']

列表是一种可变的数据类型,也就是可以对列表的的元素可以进行修改,这与字符串有显著的差别,对字符串类型进行修改后,都会返回新的字符串

1.2 访问列表中的值

如果访问列表中的某个值,使用下标索引来访问列表中的值,与字符串一样使用方括号的形式截取字符,示例代码如下:

?
1
2
3
4
5
list1 = ["Hello", "一碗周", "你好"]
# 列表的索引
print(list1[1])  # 一碗周
# 列表的切片
print(list1[1:3])  # ['一碗周', '你好']

1.3 列表的运算符

表和字符串类型一样,同样支持拼接、重复、成员运算等操作,

示例代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
list1 = ["Hello"]
list2 = ["World"]
list3 = [1, 2, 3, 4, 5]
list4 = list(range(1, 6))
 
list5 = list1 + list2  # ['Hello', 'World']
print(list5)
 
list6 = list1 * 3  # ['Hello', 'Hello', 'Hello']
list7 = list3 * 2  # [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
print(list6)
print(list7)
 
print("W" in list1)  # False
print("W" in list2)  # False
 
# 列表的比较运算
# 两个列表比较相等性比的是对应索引位置上的元素是否相等
print(list3 == list4)  # True
list8 = list(range(1, 7))
print(list3 == list8)  # False

1.4 列表元素的遍历

遍历列表同遍历字符串是一样的,示例代码如下:

?
1
2
3
4
5
6
7
list1 = ["H", "e", "l", "l", "o"]
# 方法1
for index in range(len(list1)):
    print(list1[index])
# 方法2
for ch in list1:
    print(ch)

2、列表的方法

2.1 添加和删除元素

直接上代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
list1 = ["cute", "beautiful", "一碗周"]
 
# append()在列表尾部添加元素
list1.append("lovely")
print(list1)  # ['cute', 'beautiful', '一碗周', 'lovely']
 
# insert()在列表指定索引位置插入元素
list1.insert(2, "prefect")
print(list1)  # ['cute', 'beautiful', 'prefect', '一碗周', 'lovely']
 
# remove()删除指定元素
list1.remove("lovely")
print(list1)  # ['cute', 'beautiful', 'prefect', '一碗周']
 
# pop()删除指定索引位置的元素
list1.pop(2)
print(list1)  # ['cute', 'beautiful', '一碗周']
 
# clear()清空列表中的元素
list1.clear()
print(list1)  # []

Python中也可以使用del关键字对列表元素进行删除,类似于pop,示例代码↓

?
1
2
3
4
5
6
7
8
list1 = ["cute", "beautiful", "甜甜"]
 
del list1[1]
print(list1) # ['cute', '甜甜']
 
# 删除整个列表
del list1
print(list1) # NameError: name 'list1' is not defined

2.2 元素位置和次数

使用index()来查找元素的位置,使用count()来统计元素出现的次数

?
1
2
3
4
5
6
7
8
9
list1 = ["beautiful", "cute", "beautiful",
         'prefect', "beautiful", "一碗周", 'lovely']
# 查找"beautiful"第一次出现的位置
print(list1.index("beautiful"))  # 0
# 从第四个元素以后查找"beautiful"最近一次出现的位置
print(list1.index("beautiful", 3))  # 4
 
# 统计"beautiful"出现的次数
print(list1.count("beautiful"))  # 3

2.3 元素排序和反转

使用sort()方法可以实现列表元素的排序,而reverse()方法可以实现元素的反转,示例代码↓

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
list1 = ["cute", "beautiful", "一碗周"]
list2 = list(range(10))
 
# 排序
list1.sort()
print(list1)  # ['beautiful', 'cute', '一碗周']
 
# 反转
list1.reverse()
print(list1)  # ['一碗周', 'cute', 'beautiful']
 
list2.reverse()
print(list2)  # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
 
# 前面的操作原来的列表进行修改,如果不让原来的数据被破坏可以使用copy()备份一份
list3 = list2.copy()
list3.sort()
print(list2)  # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
print(list3)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

2.4 小结

Python列表的定义及使用

Python列表的定义及使用

3、列表的生成式

要求:为字符串123和字符串ABC创建一个笛卡尔积构成的列表,示例代码如下:

原始方法:

?
1
2
3
4
5
6
7
a = "123"
b = "ABC"
list1 = []
for x in a:
     for y in b:
          list1.append(x + y)
print(list1) # ['1A', '1B', '1C', '2A', '2B', '2C', '3A', '3B', '3C']

生成列方法:

?
1
2
3
4
a = "123"
b = "ABC"
list1 = [x + y for x in a for y in b]
print(list1) # ['1A', '1B', '1C', '2A', '2B', '2C', '3A', '3B', '3C']

这中方法不仅代码量少,而且性能上也要优于普通的for循环和append追加的方式

4、嵌套的列表

因为列表里面的变量可以存储多种数据类型,就出现了列表里面有列表的时候,称之为列表的嵌套,示例代码如下:

?
1
2
3
4
5
6
7
list1 = [["cute", "beautiful", "一碗周"], "cute", "beautiful", "一碗周"]
 
print(list1[0])  # ['cute', 'beautiful', '一碗周']
print(list1[1])  # cute
 
# 如果想要查看被嵌套的那个cute则需要使用多个索引值
print(list1[0][0])  # cute

不管嵌套多少都是同理的

到此这篇关于Python列表的定义及使用的文章就介绍到这了,更多相关Python列表定义及使用内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/7022986018332606471

延伸 · 阅读

精彩推荐