数据结构:
数据结构是通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合,这些元素可以是数字或者字符,甚至可以是其他数据结构(摘自python基础教程)
在python中最基本的数据结构是序列,python中包含6中内键的序列,最常使用的两种类型是:列表和元组;其他的四种序列为字符串、Unicode字符串、buffer对象和xrange对象
列表[list]:
列表是Python中最基本的数据结构,列表中每个元素都分配一个位置进行编号--从0开始递增。元素可通过这些编号进行访问(索引),这些元素从左到右从0开始计数,也可以从右至左进行计数,最右边的位置为-1
列表也称之为数组,数组可分为一维数组,多维数组
列表可修改
#一维数组
list = ['Denny','Jenny','Liming','Lilei','Hanmeimei']
#多维数组
#二维数组
list1=[1,2,3,4,[51,52,53,54],6,,78]
#三维数组
list2=[1,2,3,4,[xiaoming[18,175],xiaola# 列list = ['吉娃娃1','吉娃娃2','吉娃娃3','吉娃娃4']
a1 = list[0]
a2 = list[1]
a3 = list[2]
a4 = list[3]
# 按下标取值标
print('第0个元素:',a1 )
print('第1个元素:',a2)
print('第2个元素:',a3)
print('第3个元素:',a4)
运行结果:
D:\Python\Miniconda3\python.exe
第0个元素: 吉娃娃1
第1个元素: 吉娃娃2
第2个元素: 吉娃娃3
第3个元素: 吉娃娃4
Process finished with exit code 0
列表的基本操作:增、删、改、查
增:
1 # 增
2 方式一:
3 list = ['Denny','Jenny','Lilei','Hanmeimei']
4 #append默认在最后的位置添加元素
5 list.append('Liming')
6 print(list)
7
8 运行结果:
9 D:\Python\Miniconda3\python.exe 10 ['Denny', 'Jenny', 'Lilei', 'Hanmeimei', 'Liming']
11 Process finished with exit code 0
12
13
14 方式二:
15 list = ['Denny','Jenny','Lilei','Hanmeimei']
16 #在指定位置中填写下标
17 list.insert(2,'Liming')
18 print(list)
19
20 运行结果:
21 D:\Python\Miniconda3\python.exe 22 ['Denny', 'Jenny', 'Liming', 'Lilei', 'Hanmeimei']
23 Process finished with exit code 0
删
# 删
方式一:
#删除指定位置的元素
list = ['Denny','Jenny','Liming','Lilei','Hanmeimei']
del list[3]
print(list)
运行结果:
D:\Python\Miniconda3\python.exe
['Denny', 'Jenny', 'Liming', 'Hanmeimei']
Process finished with exit code 0
方式二:
list = ['Denny','Jenny','Liming','Lilei','Hanmeimei']
#pop不写指定位置默认删除最后的元素
list.pop()
print(list)
运行结果:
D:\Python\Miniconda3\python.exe
['Denny', 'Jenny', 'Liming', 'Lilei']
Process finished with exit code 0
方式三:
list = ['Denny','Jenny','Liming','Lilei','Hanmeimei']
#pop也可以删除指定位置的元素
list.pop(1)
print(list)
运行结果:
D:\Python\Miniconda3\python.exe
['Denny', 'Liming', 'Lilei', 'Hanmeimei']
Process finished with exit code 0
方式四:
list = ['Denny','Jenny','Liming','Lilei','Hanmeimei']
#remove删除指定元素
list.remove('Jenny')
print(list)
运行结果:
D:\Python\Miniconda3\python.exe
['Denny', 'Liming', 'Lilei', 'Hanmeimei']
Process finished with exit code 0
方式五:
list = ['Denny','Jenny','Liming','Lilei','Hanmeimei']
#清空列表
list.clear()
print(list)
运行结果:
D:\Python\Miniconda3\python.exe
[]
Process finished with exit code 0
改
#改
list = ['Denny','Jenny','Liming','Lilei','Hanmeimei']
list[1] = 'xiaoming'
print(list)
运行结果:
D:\Python\Miniconda3\python.exe
['Denny', 'xiaoming', 'Liming', 'Lilei', 'Hanmeimei']
Process finished with exit code 0
查
list = ['Denny','Jenny','Liming','Lilei','Hanmeimei']
#根据下标进行查询
a = list[1]
#-1取最后一个元素
b = list[-1]
print(a)
print(b)
运行结果:
D:\Python\Miniconda3\python.exe
Jenny
Hanmeimei
Process finished with exit code 0
切片
切片操作可以用来访问一定范围内的元素
#切片:顾头不顾尾
[n:m:q] n/m为下标,q为步长
栗子1:
list = [0,1,2,3,4,5,6,7,8,9]
list1 = list[1:4]
list2 = list[1:8:2]
print(list1)
print(list2)
运行结果:
D:\Python\Miniconda3\python.exe
[1, 2, 3]
[1, 3, 5, 7]
Process finished with exit code 0
栗子2:
#当步长为负数时,只能从右向左提取元素
list = [0,1,2,3,4,5,6,7,8,9]
list1 = list[5:1:-1]
list2 = list[5:1:-2]
print(list1)
print(list2)
运行结果:
D:\Python\Miniconda3\python.exe
[5, 4, 3, 2]
[5, 3]
Process finished with exit code 0
列表还有一些常用的内置函数
list1 = ['Denny','Jenny','Lilei','Hanmeimei']
list2 = ['hello!','goodday','newday']
#extend 扩展,将list2中的元素添加到list1中,list1中的元素已经改变
list1.extend(list2)
print(list1)
运行结果:
D:\Python\Miniconda3\python.exe
['Denny', 'Jenny', 'Lilei', 'Hanmeimei', 'hello!', 'goodday', 'newday']
Process finished with exit code 0
“+”号连接两个列表,注意和extend的区别
list1 + list2
list3 = list1 + list2
print(list1)
print(list3)
运行结果:
D:\Python\Miniconda3\python.exe
['Denny', 'Jenny', 'Lilei', 'Hanmeimei']
['Denny', 'Jenny', 'Lilei', 'Hanmeimei', 'hello!', 'goodday', 'newday']
Process finished with exit code 0
list2 = ['hello!','goodday','newday','hello!']
#count获取元素出现的次数
print(list2.count('hello!'))
运行结果:
D:\Python\Miniconda3\python.exe
2
Process finished with exit code 0
list2 = ['hello!','goodday','newday','hello!']
#index用于从列表中找出某个值第一个匹配项的下标,当元素不存在的时候就会报错
print(list2.index('hello!'))
运行结果:
D:\Python\Miniconda3\python.exe
0
Process finished with exit code 0
list = ['Denny','Jenny','Liming','Lilei','Hanmeimei']
#反转
list.reverse()
print(list)
运行结果:
D:\Python\Miniconda3\python.exe
['Hanmeimei', 'Lilei', 'Liming', 'Jenny', 'Denny']
Process finished with exit code 0
#排序
#sort按ASCii码升序排列,如果要进行降序排列则需要reverse=True
list1 = [2,5,3,1,8,5]
list1.sort()
print(list1)
运行结果:
[1, 2, 3, 5, 5, 8]
list2 = [2,5,3,1,8,5]
list2.sort(reverse = True)
print(list2)
运行结果:
[8, 5, 5, 3, 2, 1]