列表的基本使用

本贴最后更新于 425 天前,其中的信息可能已经时移世易

一、列表的特性

1、有序性

2、正序索引从0开始,倒序索引从-1开始

3、列表里的数据可以重复

4、列表里的元素的类型可以不一样

5、列表可以嵌套

test_list = [1,2,3,4,5,"字符串",[1,2]]

6、可以迭代


test_list = [1,2,3,4,5,"字符串",[1,2]]
for test in test_list:
    print(test)
结果:
1
2
3
4
5
字符串
[1, 2]

7、列表是可变的

二、创建列表

test_list = []

三、查询

1、可以通过索引查找元素

test_list = [1,2,3,2,4,5,"字符串",[1,2]]
result1 = test_list[-2]
print(result1)

结果:

字符串

2、可以通过元素找索引

test_list = [1,2,3,2,4,5,"字符串",[1,2]]
result2 = test_list.index("字符串")
print(result2)

结果:6

如果元素有重复,返回的是找到的第一个元素的索引值

result3 = test_list.index(2)
print(result3)

结果:1

3、统计某个元素出现的次数

test_list = [1,2,3,2,4,5,"字符串",[1,2]]

result4 = test_list.count(2)#数据类型要对上,2是数值类型,不能传字符串

print(result4)

结果:2

4、查询列表长度

test_list = [1,2,3,2,4,5,"字符串",[1,2]]

print(len(test_list))

结果:8

5、查询数据类型

test_list = [1,2,3,2,4,5,"字符串",[1,2]]

print(type(test_list))

结果:<class 'list'>

6、切片

语法:test_list = [起始索引:结束索引:步长]

(1)【正序切片】

test_list = [1,2,3,2,4,5,"字符串",[1,2]]

result = test_list[::2]

print(result)

result = test_list[2::1]

print(result)

结果:

[1, 3, 4, '字符串']

[3, 2, 4, 5, '字符串', [1, 2]]

(2)【倒序切片】

test_list = [1,2,3,2,4,5,"字符串",[1,2]]

result = test_list[-1:-3:-1]

print(result)

结果:

[[1, 2], '字符串']

(3)【反转】

test_list = [1,2,3,2,4,5,"字符串",[1,2]]

result = test_list[::-1]

print(result)

结果:

[[1, 2], '字符串', 5, 4, 2, 3, 2, 1]

注意点:

A.步长不写默认为1

B.结束索引不写默认为列表长度

C.起始索引不写默认为0

D.索引的取值左闭右开(取头不取尾)

E.步长前面为负(-)表示倒序切片,为正(+)表示正序切片(+可以不写)

F.正序切片用正序索引,倒序切片用倒序索引

G.列表中的空格也占一个索引位置(空格也是列表的组成部分)

四、修改

test_list = [1,2,3,2,4,5,"字符串",[1,2]]

1、通过索引修改对应元素

test_list[0] = "元宵节"

print(test_list)

结果:

['元宵节', 2, 3, 2, 4, 5, '字符串', [1, 2]]

2、写入元素

在list末尾添加元素

test_list.append("元旦")

print(test_list)

结果:

['元宵节', 2, 3, 2, 4, 5, '字符串', [1, 2], '元旦']

3、插入

将元宵节插入到索引位置6,其他字符串往后推

test_list = [1,2,3,2,4,5,"字符串",[1,2]]

test_list.insert(6,"春节")

print(test_list)

结果:

[1, 2, 3, 2, 4, 5, '春节', '字符串', [1, 2]]

4、列表合并

(1)【通过+会产生新的列表】

test_list1 = [1,2,3,2,4,5,"字符串",[1,2]]

test_list2 = ["py48","元宵节快乐"]

result = test_list1 + test_list2

print(result)

结果:

[1, 2, 3, 2, 4, 5, '字符串', [1, 2], 'py48', '元宵节快乐']

(2)【通过extend的方式】

不会产生新的列表

test_list1 = [1,2,3,2,4,5,"字符串",[1,2]]

test_list2 = ["py48","元宵节快乐"]

将test_list2的元素,通过追加的方式写到test_list1中去:

test_list1.extend(test_list2)

print(test_list1)

结果:

[1, 2, 3, 2, 4, 5, '字符串', [1, 2], 'py48', '元宵节快乐']

5、删除

(1)通过索引删除对应元素

列表.pop(index) 返回的是被删除的索引对应的值

test_list1 = [1,2,3,2,4,5,"字符串",[1,2]]

result = test_list1.pop(-1)#列表方法

print(test_list1)

print(result)

结果:

[1, 2, 3, 2, 4, 5, '字符串']
[1, 2]

索引值不能超过列表长度,否则会报错

result = test_list1.pop(20)#报错

结果:

Traceback (most recent call last):
File "D:\学习\Python自动化资料-柠檬班\pythonProject-py48\111111.py", line 78, in
result = test_list1.pop(20)
IndexError: pop index out of range

(2)通过del删除,没有返回值

test_list = [1,2,3,2,4,5,"字符串",[1,2]]

del test_list[6]#系统方法

print(test_list)

结果:

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

(3)移除

列表.remove(value),如果value有重复,会移除出现的第一个value

test_list = [1,2,3,2,4,5,"字符串",[1,2]]

test_list.remove(2)#移除第一个2

print(test_list)

结果:

[1, 3, 2, 4, 5, '字符串', [1, 2]]

(4)清空列表

列表.clear()

test_list = [1,2,3,2,4,5,"字符串",[1,2]]

test_list.clear()

print(test_list)

结果:

[]

6、成员运算

(1)in

test_list = [1,2,3,2,4,5,"字符串",[1,2]]

print(2 in test_list)

结果:True

(2)not in

print(2 not in test_list)

结果:False

7、排序

(1)排序原理

通过ASCII码大小进行排序

#获取字符的ASCII码

print(ord("a"))

#通过ASCII码获取对应字符

print(chr(97))

#词语没有ascii码

print(ord("会话"))#报错

(2)在原有的基础上进行排序,修改原列表

列表.sort(reverse=True[False])

reverse=True:降序

reverse=False:升序

默认为False

test_list = [99,44,55,23,13,3,5,9,12,1]

test_list.sort(reverse=True)#降序排序,默认reverse=False

print(test_list)

结果:[99, 55, 44, 23, 13, 12, 9, 5, 3, 1]

(3)不修改原列表,生成新的列表

test_list = [99,44,55,23,13,3,5,9,12,1]

result = sorted(test_list,reverse=True)

print(result)

print(test_list)

结果:

[99, 55, 44, 23, 13, 12, 9, 5, 3, 1]
[99, 44, 55, 23, 13, 3, 5, 9, 12, 1]

8、列表反转

test_list = [99,44,55,23,13,3,5,9,12,1]

test_list.reverse()

print(test_list)

结果:[1, 12, 9, 5, 3, 13, 23, 55, 44, 99]

9、其他方法

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

(1)最大值

result = max(test_list)

结果:5

(2)最小值

result1 = min(test_list)

结果:1

(3)求和

result2 = sum(test_list)

结果:15

(4)列表推导式

求1~100的和

test_list = [i for i in range(1,101)]

print(sum(test_list))

print(len(test_list))

结果:

5050
100

(5)长度len()

回帖
请输入回帖内容 ...