本文共 8245 字,大约阅读时间需要 27 分钟。
说明:
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
·进阶演示:count()可以比较字符串中是否含有包含的字符串
>>> name = 'xpleaf'>>> name.count('xp')1>>> name.count('leaf')1>>> name.count('xpleaf123')0===>如果包含有查找的字符串,则返回1,否则返回0
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']
7.pop()参数
·基本功能:将列表中最后一个元素删除
·基本语法:
names.pop()
·演示如下:
>>> 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']>>> names.pop()'CL'>>> 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]
8.remove()参数
·基本功能:删除列表中指定内容的元素
·基本语法:
names.remove('love') ===>删除列表中'love'元素
·演示如下:
>>> 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]>>> names.remove('love')>>> names['xpleaf_Yip', '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_Yip', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
·如果存在多个相同的元素,则只删除第1个:
>>> names.remove(0)>>> names['xpleaf_Yip', 'yonghaoyip', 'CL', 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_Yip', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
9.sort()参数
·基本功能:以ASCII表的顺序对列表中的元素进行排序,纯数值元素会在前面
·基本语法:
names.sort()
·演示如下:
>>> names.sort()>>> names[0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'CL', 'xpleaf_Yip', 'xpleaf_Yip', 'yonghaoyip']
·如果有多个元素的字符串,则按字符串中的字符依次顺序比较,直到有不相同的为止;
10.reverse()参数
·基本功能:将原来列表中的元素倒序存储
·基本语法:
names.reverse()
·演示如下:
>>> names.reverse()>>> names['yonghaoyip', 'xpleaf_Yip', 'xpleaf_Yip', 'CL', 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0]
11.与列表相关的其它操作
--删除列表中的元素:del
·不用remove()的方式删除,需要指定索引号;
·演示如下:
>>> names['yonghaoyip', 'xpleaf_Yip', 'xpleaf_Yip', 'CL', 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0]>>> names[2]'xpleaf_Yip'>>> del names[2]>>> names['yonghaoyip', 'xpleaf_Yip', 'CL', 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0]
--将字符串内容变为列表中的元素:list()
·演示如下:
>>> import string>>> string.ascii_lowercase'abcdefghijklmnopqrstuvwxyz'>>> a = list(string.ascii_lowercase)>>> a['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
--将列表中的元素连接为字符串:''.join()
·str()只会将整个列表作为一个字符串:
>>> str(a)"['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']"
·把列表元素连接成字符串方法如下:
>>> ''.join(a)'abcdefghijklmnopqrstuvwxyz'>>> '_'.join(a)'a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z'
·''.join()参数中的列表元素只能是字符串型,不能是数值型:
>>> str(names)"['yonghaoyip', 'xpleaf_Yip', 'CL', 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0]">>> ''.join(names)Traceback (most recent call last): File "", line 1, in TypeError: sequence item 3: expected string, int found
12.元组的提及
·元组跟列表一样,但元组内容一旦生成,无法修改;
·元组主要是用来存储别人不能修改的内容;
·元组的相关操作演示和说明如下:
>>> tuple = ('a', 'b', 'c', 1, 2, 3)>>> tuple('a', 'b', 'c', 1, 2, 3)>>> tuple[3] ===>取特定一个元素时,与列表类似1>>> tuple[3] = 'b' ===>不能修改元组的元素内容Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment>>> del tuple[3] ===>也不能删除元组中的某一元素Traceback (most recent call last): File " ", line 1, in TypeError: 'tuple' object doesn't support item deletion>>> list(tuple)['a', 'b', 'c', 1, 2, 3]>>> tuple. ===>按tab键后可以看到只有count()和index()参数与列表相似……tuple.count(tuple.index(
·将元组变为列表时可使用list()函数。
转载地址:http://alazl.baihongyu.com/