我是靠谱客的博主 炙热大米,这篇文章主要介绍Python之列表的操作及拓展python之列表列表的相关操作,现在分享给大家,希望可以做个参考。

python之列表

列表的格式

  • list = [‘a’, ‘b’, ‘c’]

    列表中的元素可以是不同类型的,比C语言强大

  • list = [1, ‘b’]

列表可以使用下标取值

复制代码
1
2
3
4
5
6
7
8
9
namesList = ['xiaoWang','xiaoZhang','xiaoHua'] print(namesList[0]) print(namesList[1]) print(namesList[2]) 结果: xiaoWang xiaoZhang xiaoHua

使用for循环遍历列表

复制代码
1
2
3
4
5
6
7
8
demo: list = ['a', 'b', 'c'] for name in list: print(name) a b c

使用while循环

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
demo: list = ['a', 'b', 'c'] length = len(list) i = 0 while i < length: print(list[i]) i += 1 结果: a b c

列表的相关操作

添加元素(“增”append,extend,insert)

append() 追加单个元素到listA尾部,只接受一个参数,参数可以是任何数据类型,被追加的元素在listA中保持原结构类型

复制代码
1
2
3
4
5
6
7
8
demo: #定义变量A,默认有3个元素 listA = ['a', 'b', 'c'] listA.append('d') print(listA) 结果: ['a', 'b', 'c', 'd']

extend() 将一个列表中每个元素分别添加到另一个列表中,只接受一个参数,extend相当于是将listB链接到listA中

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
demo1: listA = ['a', 'b', 'c'] listB = [1, 2] listA.extend(listB) print(listA) 结果: ['a', 'b', 'c', 1, 2] demo2: listA = ['a', 'b', 'c'] listB = [1, 2] listA.append(listB) print(listA) 结果: ['a', 'b', 'c', [1, 2]]

insert() 是将一个元素插入到列表的指定坐标中,

复制代码
1
2
3
4
5
6
7
8
公式:insert(坐标, "元素") demo: listA = ['a', 'b', 'c'] listA.insert(1, 'e') print(listA) 结果: ['a', 'e', 'b', 'c']

加号,+可以将两个list相加,会得到一个新的list列表

复制代码
1
2
3
4
5
6
7
8
demo: listA = ['a', 'b', 'c'] listB = [1, 2] listC = listA + listB print(listC) 结果: ['a', 'b', 'c', 1, 2]

注意:append,extend,insert都没有返回值,是对列表的操作,不返回新的对象,加号是返回新的对象,尽量不要采用这种方法。

修改元素(根据下标修改)

复制代码
1
2
3
4
5
6
7
demo: listA = ['a', 'b', 'c'] listA[2] = 'f' print(listA) 结果: ['a', 'b', 'f']

查找元素(in, not in, index, count)

in 判断元素是都在列表中,是返回True,否返回False

复制代码
1
2
3
4
5
6
7
8
9
demo: listA = ['a', 'b', 'c'] if 'f' in listA: print('True') else: print('False') 结果: False

not in判断元素是否不在列表中,与 in 的含义相反。

index和count的用法相同,但是返回值不同

count返回的是元素出现的次数,不存在返回0

复制代码
1
2
3
4
5
6
7
demo: listA = ['a', 'b', 'c'] A = listA.count('c') print(A) 结果: 1

index返回的是元素下标,是元素第一次出现的位置的下标。不存在报错

复制代码
1
2
3
4
5
6
7
demo2: listA = ['c', 'a', 'b', 'c'] A = listA.index('c') print(A) 结果: 0

index还可以应用到切片中,存在的话返回元素在切片中出现的第一个位置的下标,不存在报错。

复制代码
1
2
3
4
5
6
7
demo: listA = ['a', 'b', 'c', 'd'] A = listA.index('c', 0, 3) print(A) 结果: 2

删除元素(‘删’ del, pop, remove)

del 根据下标进行删除

复制代码
1
2
3
4
5
6
7
demo: listA = ['a', 'b', 'c'] del listA[2] print(listA) 结果: ['a', 'b']

remove 删除指定元素,没有该元素报错

复制代码
1
2
3
4
5
6
7
demo: listA = ['a', 'b', 'c'] listA.remove('b') print(listA) 结果: ['a', 'c']

pop 方法删除元素,当()里面没有索引值时,默认删除最后一个元素

复制代码
1
2
3
4
5
6
7
8
9
10
demo: listA = ['a', 'b', 'c'] listA.pop(0) print(listA) listA.pop() print(listA) 结果: ['b', 'c'] ['b']

排序(sort, reverse)

sort方法是将list按特定顺序重新排列,默认为由小到大,参数reverse=True可改为倒序,由大到小。

reverse方法是将list逆置。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
demo: listA = [1, 4, 5, 2, 3, 9, 6] listA.reverse() print(listA) listA.sort() print(listA) listA.sort(reverse=True) print(listA) 结果: [6, 9, 3, 2, 5, 4, 1] [1, 2, 3, 4, 5, 6, 9] [9, 6, 5, 4, 3, 2, 1]

列表排序拓展

sorted() 函数

简单的升序排序:调用sorted方法,返回新的list

复制代码
1
2
3
4
5
6
7
demo: listA = [1, 4, 5, 2, 3, 9, 6] listA = sorted(listA) print(listA) 结果: [1, 2, 3, 4, 5, 6, 9]

注意:打印的listA和原listA已经不是一个对象了

key参数/函数

key参数的值作为一个函数,此函数只有一个参数且返回一个值用来进行比较。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
demo: listA = [ ('wang', 'A', '25'), ('liu', 'B', '27'), ('zhao', 'A', '24'), ('zhou', 'C', '28'), ] listB = sorted(listA, key=lambda age: age[2]) print(listB) 结果: [('zhao', 'A', '24'), ('wang', 'A', '25'), ('liu', 'B', '27'), ('zhou', 'C', '28')]

Operator模块函数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
demo: from operator import itemgetter, attrgetter listA = [ ('wang', 'A', '25'), ('liu', 'B', '27'), ('zhao', 'A', '24'), ('zhou', 'C', '28'), ] listB = sorted(listA, key=itemgetter(2)) print(listB) 结果: [('zhao', 'A', '24'), ('wang', 'A', '25'), ('liu', 'B', '27'), ('zhou', 'C', '28')]

列表的嵌套

一个列表中的元素又是一个列表,这就是列表的嵌套

应用:

一个学校,有3个宿舍,现在有8位学生等待宿舍的分配,请编写程序,完成随机的分配

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
demo: import random #定义列表用来保存3个宿舍 dormitory_list = [[], [], []] #定义一个列表用来存储8位学生的名字 student_list = ['zhao', 'qian', 'sun', 'li', 'zhou', 'wu', 'zheng', 'wang'] i = 0 for name_s_l in student_list: dormitory_index = random.randint(0, 2) dormitory_list[dormitory_index].append(name_s_l) print('* '*20) i = 1 for dormitory_name in dormitory_list: print('宿舍%d的人数为:%d' % (i, len(dormitory_name))) i += 1 for name in dormitory_name: print('%s ' % name, end='') print('n') print('* '*20)

最后

以上就是炙热大米最近收集整理的关于Python之列表的操作及拓展python之列表列表的相关操作的全部内容,更多相关Python之列表内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(97)

评论列表共有 0 条评论

立即
投稿
返回
顶部