说明:
Python中的列表类似于其它高级语言中的数组,不过Python的列表操作起来要轻松很多。
Python中列表的学习主线主要是围绕对列表参数的操作使用上,重点关注的应该有如下这些:
names.append( names.count( names.extend( names.index( names.insert( names.pop( names.remove( names.reverse( names.sort(
下面的内容基本上都是围绕上面的操作进行说明。
1.基本操作
·基本的操作主要有如下:
names[num]:排序为数字num的元素 names[-1:]:最后1个元素 names[-5:]:最后5个元素 names[-5:-1]:最后5个元素,但不包括最后1个元素(即不包括names[-1]) names[:5]:开始5个元素(即不包括names[5]) names[0:5]:开始5个元素(即不包括names[5]) names[10:15]:names[10]到names[14]
·上面有取范围的情况,前后两个数字,可以记为:“虎头蛇尾”,即取前不取后
· 以下面的环境做一个简单的测试:
>>> names = [‘xpleaf‘,‘yonghaoyip‘,‘CL‘] >>> names.extend(range(30)) >>> names [‘xpleaf‘, ‘yonghaoyip‘, ‘CL‘, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
(1)names[num]:排序为数字num的元素
>>> names[2] ‘CL‘
(2)names[-1:]:最后1个元素
>>> names[-1:] [29]
(3)names[-5:]:最后5个元素
>>> names[-5:] [25, 26, 27, 28, 29]
(4)names[-5:-1]:最后5个元素,但不包括最后1个元素(即不包括names[-1])
>>> names[-5:-1] [25, 26, 27, 28]
(5)names[:5]或names[0:5]:开始5个元素(即不包括names[5])
>>> names[:5] [‘xpleaf‘, ‘yonghaoyip‘, ‘CL‘, 0, 1] >>> names[0:5] [‘xpleaf‘, ‘yonghaoyip‘, ‘CL‘, 0, 1]
(6)names[10:15]:names[10]到names[14]
>>> names[10:15] [7, 8, 9, 10, 11] >>> names[10] 7 >>> names[14] 11 >>> names[15] 12
2.append()参数
·基本功能:向列表中添加元素
·基本语法:
names.append(‘xpleaf‘)
·append()参数在列表的最后面增加元素,一次只能添加一个;
·演示如下:
>>> names.append(‘xpleaf‘) >>> names [‘xpleaf‘, ‘yonghaoyip‘, ‘CL‘, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, ‘xpleaf‘]
3.count()参数
·基本功能:计算列表中相同元素的个数
·基本语法:
names.count(‘xpleaf‘)
·演示如下:
>>> names [‘xpleaf‘, ‘yonghaoyip‘, ‘CL‘, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, ‘xpleaf‘] >>> names.count(‘xpleaf‘) 2
4.extend()参数
·基本功能:将另一个列表中的元素合并到当前列表中
·基本语法:
names.extend(range(10)) 等价于下面的内容: names = names + range(10) ===>列表也可以直接做加法,Python中序列的特性
·演示如下:
>>> names [‘xpleaf‘, ‘yonghaoyip‘, ‘CL‘, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, ‘xpleaf‘] >>> range(10) ===>range()的输出本来就是一个列表 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> names.extend(range(10)) >>> names [‘xpleaf‘, ‘yonghaoyip‘, ‘CL‘, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, ‘xpleaf‘, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
·append()添加的是列表中的元素,如果写成下面这样:
names.append(range(10)) ===>只会在names列表末尾处添加一个列表元素[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
5.index()参数
·基本功能:找出需要查找的列表元素的索引号
·基本语法:
names.index(‘xpleaf‘)
·如果列表中有多个相同元素,index()找出的只是第1个相同元素的索引号
·演示如下:
>>> names.append(‘CL‘) >>> names [‘xpleaf‘, ‘yonghaoyip‘, ‘CL‘, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, ‘xpleaf‘, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ‘CL‘] >>> names.index(‘xpleaf‘) 0 >>> names.index(‘CL‘) 2 >>> names[2] ‘CL‘
6.inset()参数
·基本功能:在列表中某一位置插入元素
·基本语法:
names.insert(6,‘love‘) ===>在列表的索引号为6的位置中插入元素‘love‘
·演示如下:
>>> names [‘xpleaf‘, ‘yonghaoyip‘, ‘CL‘, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, ‘xpleaf‘, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ‘CL‘] >>> names.insert(6,‘love‘) >>> names [‘xpleaf‘, ‘yonghaoyip‘, ‘CL‘, 0, 1, 2, ‘love‘, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, ‘xpleaf‘, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ‘CL‘] >>> names[6] ‘love‘
--小的综合操作:将上面列表中的‘xpleaf‘替换为‘xpleaf_Yip‘
·未充分利用Python特性的笨方法:
>>> while True: ... if names.count(‘xpleaf‘) == 0: ... break ... else: ... names[names.index(‘xpleaf‘)] = ‘xpleaf_Yip‘ ... >>> names [‘xpleaf_Yip‘, ‘yonghaoyip‘, ‘CL‘, 0, 1, 2, ‘love‘, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, ‘xpleaf_Yip‘, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ‘CL‘]
·虽然可以达到目的,但实际上该方法是比较笨的;
·充分利用Python特性的方法:
>>> for i in range(names.count(‘xpleaf‘)): ... names[names.index(‘xpleaf‘)] = ‘xpleaf_Yip‘ ... >>> names [‘xpleaf_Yip‘, ‘yonghaoyip‘, ‘CL‘, 0, 1, 2, ‘love‘, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, ‘xpleaf_Yip‘, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ‘CL‘]
<未完,continue>
时间: 2024-10-10 08:26:51