python学习Day2之数据结构

Posted amei1992

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习Day2之数据结构相关的知识,希望对你有一定的参考价值。

列表[list]:

列表是Python中最基本的数据结构,列表中每个元素都分配一个位置

列表也称之为数组,数组可分为一维数组,多维数组

#一维数组
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

 

以上是关于python学习Day2之数据结构的主要内容,如果未能解决你的问题,请参考以下文章

python学习之路-day2

Python学习笔记-Day2-Python基础之元祖操作

Python学习笔记-Day2-Python基础之字符串操作22222222222222222222222222222222

python学习笔记Day2

Python学习-day2

python 学习之路 Day2