且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

【Python之旅】第二篇(二):列表与元组

更新时间:2022-06-14 23:03:29

说明:

    Python中的列表类似于其它高级语言中的数组,不过Python的列表操作起来要轻松很多。

    Python中列表的学习主线主要是围绕对列表参数的操作使用上,重点关注的应该有如下这些:

1
2
3
4
5
6
7
8
9
names.append(
names.count(
names.extend(
names.index(
names.insert(
names.pop(
names.remove(
names.reverse(
names.sort(

    下面的内容基本上都是围绕上面的操作进行说明。




1.基本操作

    

·基本的操作主要有如下:

1
2
3
4
5
6
7
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]

·上面有取范围的情况,前后两个数字,可以记为:“虎头蛇尾”,即取前不取后

· 以下面的环境做一个简单的测试:

1
2
3
4
>>> names = ['xpleaf','yonghaoyip','CL']
>>> names.extend(range(30))
>>> names
['xpleaf''yonghaoyip''CL'01234567891011121314151617181920212223242526272829]

(1)names[num]:排序为数字num的元素

1
2
>>> names[2]
'CL'

(2)names[-1:]:最后1个元素

1
2
>>> names[-1:]
[29]

(3)names[-5:]:最后5个元素

1
2
>>> names[-5:]
[2526272829]

(4)names[-5:-1]:最后5个元素,但不包括最后1个元素(即不包括names[-1])

1
2
>>> names[-5:-1]
[25262728]

(5)names[:5]或names[0:5]:开始5个元素(即不包括names[5])

1
2
3
4
>>> names[:5]
['xpleaf''yonghaoyip''CL'01]
>>> names[0:5]
['xpleaf''yonghaoyip''CL'01]

(6)names[10:15]:names[10]到names[14]

1
2
3
4
5
6
7
8
>>> names[10:15]
[7891011]
>>> names[10]
7
>>> names[14]
11
>>> names[15]
12




2.append()参数


·基本功能:向列表中添加元素

·基本语法:

1
names.append('xpleaf')

·append()参数在列表的最后面增加元素,一次只能添加一个;

·演示如下:

1
2
3
>>> names.append('xpleaf')
>>> names
['xpleaf''yonghaoyip''CL'01234567891011121314151617181920212223242526272829'xpleaf']




3.count()参数


·基本功能:计算列表中相同元素的个数

·基本语法:

1
names.count('xpleaf')

·演示如下:

1
2
3
4
>>> names
['xpleaf''yonghaoyip''CL'01234567891011121314151617181920212223242526272829'xpleaf']
>>> names.count('xpleaf')
2

·进阶演示:count()可以比较字符串中是否含有包含的字符串

1
2
3
4
5
6
7
8
>>> name = 'xpleaf'
>>> name.count('xp')
1
>>> name.count('leaf')
1
>>> name.count('xpleaf123')
0
===>如果包含有查找的字符串,则返回1,否则返回0




4.extend()参数


·基本功能:将另一个列表中的元素合并到当前列表中

·基本语法:

1
2
3
names.extend(range(10))
等价于下面的内容:
names = names + range(10)  ===>列表也可以直接做加法,Python中序列的特性

·演示如下:

1
2
3
4
5
6
7
>>> names
['xpleaf''yonghaoyip''CL'01234567891011121314151617181920212223242526272829'xpleaf']
>>> range(10)    ===>range()的输出本来就是一个列表
[0123456789]
>>> names.extend(range(10))
>>> names
['xpleaf''yonghaoyip''CL'01234567891011121314151617181920212223242526272829'xpleaf'0123456789]

·append()添加的是列表中的元素,如果写成下面这样:

1
2
names.append(range(10))
===>只会在names列表末尾处添加一个列表元素[0123456789]




5.index()参数


·基本功能:找出需要查找的列表元素的索引号

·基本语法:

1
names.index('xpleaf')

·如果列表中有多个相同元素,index()找出的只是第1个相同元素的索引号

·演示如下:

1
2
3
4
5
6
7
8
9
>>> names.append('CL')
>>> names
['xpleaf''yonghaoyip''CL'01234567891011121314151617181920212223242526272829'xpleaf'0123456789'CL']
>>> names.index('xpleaf')
0
>>> names.index('CL')
2
>>> names[2]
'CL'




6.inset()参数


·基本功能:在列表中某一位置插入元素

·基本语法:

1
names.insert(6,'love') ===>在列表的索引号为6的位置中插入元素'love'

·演示如下:

1
2
3
4
5
6
7
>>> names
['xpleaf''yonghaoyip''CL'01234567891011121314151617181920212223242526272829'xpleaf'0123456789'CL']
>>> names.insert(6,'love')
>>> names
['xpleaf''yonghaoyip''CL'012'love'34567891011121314151617181920212223242526272829'xpleaf'0123456789'CL']
>>> names[6]
'love'


--小的综合操作:将上面列表中的'xpleaf'替换为'xpleaf_Yip'

·未充分利用Python特性的笨方法:

1
2
3
4
5
6
7
8
>>> while True:
...   if names.count('xpleaf') == 0:
...     break
...   else:
...     names[names.index('xpleaf')] = 'xpleaf_Yip'
... 
>>> names
['xpleaf_Yip''yonghaoyip''CL'012'love'34567891011121314151617181920212223242526272829'xpleaf_Yip'0123456789'CL']

·虽然可以达到目的,但实际上该方法是比较笨的;


·充分利用Python特性的方法:

1
2
3
4
5
>>> for in range(names.count('xpleaf')):
...   names[names.index('xpleaf')] = 'xpleaf_Yip'
... 
>>> names
['xpleaf_Yip''yonghaoyip''CL'012'love'34567891011121314151617181920212223242526272829'xpleaf_Yip'0123456789'CL']




7.pop()参数


·基本功能:将列表中最后一个元素删除

·基本语法:

1
names.pop()

·演示如下:

1
2
3
4
5
6
>>> names
['xpleaf_Yip''yonghaoyip''CL'012'love'34567891011121314151617181920212223242526272829'xpleaf_Yip'0123456789'CL']
>>> names.pop()
'CL'
>>> names
['xpleaf_Yip''yonghaoyip''CL'012'love'34567891011121314151617181920212223242526272829'xpleaf_Yip'0123456789]




8.remove()参数


·基本功能:删除列表中指定内容的元素

·基本语法:

1
names.remove('love')    ===>删除列表中'love'元素

·演示如下:

1
2
3
4
5
>>> names
['xpleaf_Yip''yonghaoyip''CL'012'love'34567891011121314151617181920212223242526272829'xpleaf_Yip'0123456789]
>>> names.remove('love')
>>> names
['xpleaf_Yip''yonghaoyip''CL'01234567891011121314151617181920212223242526272829'xpleaf_Yip'0123456789]

·如果存在多个相同的元素,则只删除第1个:

1
2
3
>>> names.remove(0)
>>> names
['xpleaf_Yip''yonghaoyip''CL'1234567891011121314151617181920212223242526272829'xpleaf_Yip'0123456789]




9.sort()参数


·基本功能:以ASCII表的顺序对列表中的元素进行排序,纯数值元素会在前面

·基本语法:

1
names.sort()

·演示如下:

1
2
3
>>> names.sort()
>>> names
[01122334455667788991011121314151617181920212223242526272829'CL''xpleaf_Yip''xpleaf_Yip''yonghaoyip']

·如果有多个元素的字符串,则按字符串中的字符依次顺序比较,直到有不相同的为止;




10.reverse()参数


·基本功能:将原来列表中的元素倒序存储

·基本语法:

1
names.reverse()

·演示如下:

1
2
3
>>> names.reverse()
>>> names
['yonghaoyip''xpleaf_Yip''xpleaf_Yip''CL'29282726252423222120191817161514131211109988776655443322110]




11.与列表相关的其它操作


--删除列表中的元素:del

·不用remove()的方式删除,需要指定索引号;

·演示如下:

1
2
3
4
5
6
7
>>> names
['yonghaoyip''xpleaf_Yip''xpleaf_Yip''CL'29282726252423222120191817161514131211109988776655443322110]
>>> names[2]
'xpleaf_Yip'
>>> del names[2]
>>> names
['yonghaoyip''xpleaf_Yip''CL'29282726252423222120191817161514131211109988776655443322110]


--将字符串内容变为列表中的元素:list()

·演示如下:

1
2
3
4
5
6
>>> 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()只会将整个列表作为一个字符串:

1
2
>>> 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']"

·把列表元素连接成字符串方法如下:

1
2
3
4
>>> ''.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()参数中的列表元素只能是字符串型,不能是数值型:

1
2
3
4
5
6
>>> 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 "<stdin>", line 1in <module>
TypeError: sequence item 3: expected string, int found




12.元组的提及


·元组跟列表一样,但元组内容一旦生成,无法修改;

·元组主要是用来存储别人不能修改的内容;

·元组的相关操作演示和说明如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>> tuple = ('a''b''c'123)
>>> tuple
('a''b''c'123)
>>> tuple[3]    ===>取特定一个元素时,与列表类似
1
>>> tuple[3] = 'b'    ===>不能修改元组的元素内容
Traceback (most recent call last):
  File "<stdin>", line 1in <module>
TypeError: 'tuple' object does not support item assignment
>>> del tuple[3]    ===>也不能删除元组中的某一元素
Traceback (most recent call last):
  File "<stdin>", line 1in <module>
TypeError: 'tuple' object doesn't support item deletion
>>> list(tuple)
['a''b''c'123]
>>> tuple.    ===>按tab键后可以看到只有count()和index()参数与列表相似
……
tuple.count(
tuple.index(

·将元组变为列表时可使用list()函数。