文章目录
1. 列表基本概念
- 数组:存储同一种数据类型的集合
- 列表:存储任意数据类型的集合;打了激素的数组,可以存储任意数据类型
- 与数组相同,列表第一个元素从0开始记起
2. 列表可嵌套性
在一个列表之中还可嵌套另一个列表;可实现多层列表的嵌套
type(list)可查看类型
list = [1, 1.2, True, 'python', [1, 'today']]
>>> list=[1,2,3,'python',[1,'today']]
>>> list1=[1,3,[3,'dj',[7,8]]]
>>> list1
[1, 3, [3, 'dj', [7, 8]]]
>>> list1[-1]
[3, 'dj', [7, 8]]
>>> type(list)
<type 'list'>
>>> type(list1)
<type 'list'>
2. 列表的索引
(1)正向索引:
list[1] 列表从左向右的第2个数据
(2)反向索引:
list[-1] 列表从右网左的第1个数据
>>> list=[1,2,3,'python',[1,'today']]
>>> list1=[1,3,[3,'dj',[7,8]]]
>>> list[0]
1
>>> list[1]
2
>>> list[-1]
[1, 'today']
>>> list1[-1]
[3, 'dj', [7, 8]]
3.列表切片
>>> list=[1,2,3,'python',[1,'today']]
>>> list1=[1,3,[3,'dj',[7,8]]]
(1)list[::-1] 将列表反转
>>> list[::-1]
[[1, 'today'], 'python', 3, 2, 1]
>>> list1[-1]
[3, 'dj', [7, 8]]
>>> list1[::-1]
[[3, 'dj', [7, 8]], 3, 1]
(2)list[1:] 除列表中第1、2数据之外的其他数据
list[:2] 列表前2个数据
list[:-1] 除最后一个数据之前的所有数据
>>> list1[::-1]
[[3, 'dj', [7, 8]], 3, 1]
>>> list[1:]
[2, 3, 'python', [1, 'today']]
>>>
>>> list[1:]
[2, 3, 'python', [1, 'today']]
>>> list[:2]
[1, 2]
>>> list[:-1]
[1, 2, 3, 'python']
>>> list
[1, 2, 3, 'python', [1, 'today']]
4. 列表的重复(*)
list * 3 列表重复3次
>>> list * 3
[1, 2, 3, 'python', [1, 'today'], 1, 2, 3, 'python', [1, 'today'], 1, 2, 3, 'python', [1, 'today']]
>>> list*2
[1, 2, 3, 'python', [1, 'today'], 1, 2, 3, 'python', [1, 'today']]
5. 列表的连接(+)
list + list1 连接列表list和列表list1
>>> list+list1
[1, 2, 3, 'python', [1, 'today'], 1, 3, [3, 'dj', [7, 8]]]
>>> list+[2,8,'hello']
[1, 2, 3, 'python', [1, 'today'], 2, 8, 'hello']
>>>
6. 列表成员操作符(in)
(1)print('firewalld' in service) 判断’firewalld’是否在service列表中;若是返回值为True;否则为Flase
(2)print('ftp' not in service1) 判断’ftp’是否不在service1列表中;返回值同上
>>> service=['http','firewalld','ftp']
>>> service
['http', 'firewalld', 'ftp']
>>> 'firewalld' in service
True
>>> 'https' in service
False
>>> 'firewalld' not in service
False
>>> 'https' not in service
True
>>>
7. 列表中元素的增加(append|extend|insert)
(1)追加 增加一个
service .append('firewalld') 在列表service中追加一个元素’firewalld’
>>> service.append('nfs')
>>> service
['http', 'firewalld', 'ftp', 'nfs']
(2)拉伸 增加多个
service.extend(['iscsi','nfs']) 在列表service中追加多个元素’iscsi’,‘nfs’
>>> service.extend('https','iscsi')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: extend() takes exactly one argument (2 given)
>>> service.extend(['https','iscsi'])
>>> service
['http', 'firewalld', 'ftp', 'nfs', 'https', 'iscsi']
>>>
(3)插入 在指定位置插入
service.insert(2,'smb') 在列表service中的第二个元素插入元素’smb’
>>> service.insert(2,'smb')
>>> service
['http', 'firewalld', 'smb', 'ftp', 'nfs', 'https', 'iscsi']
>>> service.insert(0,'mountd')
>>> service
['mountd', 'http', 'firewalld', 'smb', 'ftp', 'nfs', 'https', 'iscsi']
>>>
8.列表元素的弹出(弹出并未删除)(pop)
(1)默认弹出是列表最后一个
service.pop() 弹出列表的最后一个元素
>>> service
['mountd', 'http', 'firewalld', 'smb', 'ftp', 'nfs', 'https', 'iscsi']
>>> service.pop()
'iscsi'
>>> service
['mountd', 'http', 'firewalld', 'smb', 'ftp', 'nfs', 'https']
(2)指定弹出
service.pop(0) 指定弹出列表中的0号元素;即第一个元素
>>> service.pop(0)
'mountd'
>>> service
['http', 'firewalld', 'smb', 'ftp', 'nfs', 'https']
>>> service.pop(1)
'firewalld'
>>> service
['http', 'smb', 'ftp', 'nfs', 'https']
>>>
9. 删除列表元素(remove|del)
从内存中删除一个元素
(1)service.remove('smb')
(2)del service[0]
>>> service
['http', 'smb', 'ftp', 'nfs', 'https']
>>> service.remove('smb')
>>> service
['http', 'ftp', 'nfs', 'https']
>>> service.remove('http')
>>> service
['ftp', 'nfs', 'https']
>>> del service[2]
>>> service
['ftp', 'nfs']
10. 列表元素的查看(count|index)
(1)count计数:查看列表中某一元素存在的个数
>>> services=['ftp','http','iscsi','dns','http','ftp']
>>> services
['ftp', 'http', 'iscsi', 'dns', 'http', 'ftp']
>>> services.count('ftp')
2
>>> services.count('http')
2
(2)index索引查找:查找某一元素,返回其索引,默认返回其最小索引(可指定索引范围返回索引)
>>> services.index('http')
1
>>> services.index('http',3,10)
4
11.列表元素的修改
(1)通过索引重新赋值
>>> services
['dns', 'ftp', 'ftp', 'http', 'http', 'iscsi']
>>> services[0]='https'
>>> services
['https', 'ftp', 'ftp', 'http', 'http', 'iscsi']
>>> services[-1]='ftp'
>>> services
['https', 'ftp', 'ftp', 'http', 'http', 'ftp']
(2)通过切片重新赋值
>>> services[2:]=['dns','dhcp','samba']
>>> services
['https', 'ftp', 'dns', 'dhcp', 'samba']
>>> services[:-1]=['http']
>>> services
['http', 'samba']
>>>
12. 列表元素的排序(sort)
(1)默认按照Ascii码顺序进行排序
>>> services
['ftp', 'http', 'iscsi', 'dns', 'http', 'ftp']
>>> services.sort()
>>> print(services)
['dns', 'ftp', 'ftp', 'http', 'http', 'iscsi']
(2)sort(reverse=True)按照ASCII码倒序排序
>>> services.sort(reverse=True)
>>> print(services)
['iscsi', 'http', 'http', 'ftp', 'ftp', 'dns']
>>> services.sort(reverse=False)
>>> print(services)
['dns', 'ftp', 'ftp', 'http', 'http', 'iscsi']
Python列表操作大全
本文全面介绍了Python中列表的各种操作,包括基本概念、嵌套、索引、切片、重复、连接、成员操作、元素增删改查及排序等,是Python编程学习者不可多得的参考资料。
4975

被折叠的 条评论
为什么被折叠?



