技术交流学习或者有任何问题欢迎加群

编程技术交流群 : 154514123 爱上编程      Java技术交流群 : 6128790  Java

标签:数据   一个   log   ola   ces   pro   一维数组   位置   int   

列表[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之数据结构

标签:数据   一个   log   ola   ces   pro   一维数组   位置   int   

原文:https://www.cnblogs.com/Amei1992/p/8394882.html

技术交流学习或者有任何问题欢迎加群

编程技术交流群 : 154514123 爱上编程      Java技术交流群 : 6128790  Java

广告推荐

讨论区