网站首页 > 技术文章 正文
# _*_ coding:utf-8 _*_
#########列表简介#########
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
-------------------------------------------
['trek', 'cannondale', 'redline', 'specialized']
-------------------------------------------
#访问列表元素
print(bicycles[0].title())
-------------------------------------------
Trek
-------------------------------------------
#通过将索引指定为-1,可以让python返回最后一个列表元素
print(bicycles[-1])
-------------------------------------------
specialized
-------------------------------------------
#修改、添加和删除元素
#修改元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)
-------------------------------------------
['honda', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki']
-------------------------------------------
#在列表末尾添加元素
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.append('ducati')
print(motorcycles)
-------------------------------------------
['honda', 'yamaha', 'suzuki', 'ducati']
-------------------------------------------
#在列表中插入元素
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0,'ducati')
print(motorcycles)
-------------------------------------------
['ducati', 'honda', 'yamaha', 'suzuki']
-------------------------------------------
#从列表中删除元素
#使用del语句删除元素
motorcycles = ['honda', 'yamaha', 'suzuki']
del motorcycles[0]
print(motorcycles)
-------------------------------------------
['yamaha', 'suzuki']
-------------------------------------------
#使用方法pop()删除列表末尾元素,并让你能够接着使用它
motorcycles = ['honda', 'yamaha', 'suzuki']
poppde_motorcycle = motorcycles.pop()
print(motorcycles)
print(poppde_motorcycle)
-------------------------------------------
['honda', 'yamaha']
suzuki
-------------------------------------------
#弹出列表中任何位置处的元素
motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(0)
print(first_owned)
-------------------------------------------
honda
-------------------------------------------
#根据值删除元素
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
motorcycles.remove('yamaha')
print(motorcycles)
-------------------------------------------
['honda', 'suzuki', 'ducati']
-------------------------------------------
#组织列表
#使用方法sort()对列表按字母顺序进行永久性排序
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
-------------------------------------------
['audi', 'bmw', 'subaru', 'toyota']
-------------------------------------------
#按与字母顺序相反的顺序排列列表元素
cars.sort(reverse=True)
print(cars)
-------------------------------------------
['toyota', 'subaru', 'bmw', 'audi']
-------------------------------------------
#使用函数sorted()对列表进行临时排序,如果要按与字母顺序相反的顺序显示列表,也可向函数sorted()传递参数reverse=True。
cars = ['bmw', 'audi', 'toyota', 'subaru']
print("Here is the original list:")
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
print("\nHere is the sorted list again:")
print(cars)
-------------------------------------------
Here is the original list:
['bmw', 'audi', 'toyota', 'subaru']
Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyota']
Here is the sorted list again:
['bmw', 'audi', 'toyota', 'subaru']
-------------------------------------------
#倒着打印列表
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.reverse()
print(cars)
-------------------------------------------
['subaru', 'toyota', 'audi', 'bmw']
-------------------------------------------
#确定列表的长度
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(len(cars))
-------------------------------------------
4
-------------------------------------------
#########操作列表#########
#遍历整个列表
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
-------------------------------------------
alice
david
carolina
-------------------------------------------
#创建数值列表
#使用函数range()
for value in range(1,5):
print(value)
-------------------------------------------
1
2
3
4
-------------------------------------------
#使用range()创建数字列表
number = list(range(1,6))
print(number)
-------------------------------------------
[1, 2, 3, 4, 5]
-------------------------------------------
#还可以指定步长
even_numbers = list(range(2,11,2))
print(even_numbers)
-------------------------------------------
[2, 4, 6, 8, 10]
-------------------------------------------
#如何将1~10的平方加入到一个列表中
squares = []
for value in range(1,11):
square = value**2
squares.append(square)
print(squares)
-------------------------------------------
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
-------------------------------------------
#对数字列表执行简单的统计计算
digits = [1,2,3,4,5,6,7,8,9,0]
print(min(digits))
print(max(digits))
print(sum(digits))
-------------------------------------------
0
9
45
-------------------------------------------
#列表解析
squares = [value**2 for value in range(1,11)]
print(squares)
-------------------------------------------
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
-------------------------------------------
#使用列表的一部分
#切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3]) #打印前三名队员
print(players[:4]) #没有指定起始索引,Python从开头开始提取
print(players[2:]) #提取从第3个元素到列表末尾所有元素
print(players[-3:]) #输出最后三名队员
-------------------------------------------
['charles', 'martina', 'michael']
['charles', 'martina', 'michael', 'florence']
['michael', 'florence', 'eli']
['michael', 'florence', 'eli']
-------------------------------------------
#遍历切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
for player in players[:3]:
print(player.title())
-------------------------------------------
Charles
Martina
Michael
-------------------------------------------
#复制列表
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
-------------------------------------------
My favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli']
My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'ice cream']
-------------------------------------------
#元组(不可变的列表称为元组)
#定义元组
dimensions = (200,50)
print(dimensions[0])
print(dimensions[1])
-------------------------------------------
200
50
-------------------------------------------
#遍历元组中的所有值
dimensions = (200,50)
for dimension in dimensions:
print(dimension)
-------------------------------------------
200
50
-------------------------------------------
#修改元组变量
dimensions = (400,100)
for dimension in dimensions:
print(dimension)
-------------------------------------------
400
100
-------------------------------------------
猜你喜欢
- 2024-09-23 Python数据类型之列表list(python列表list函数)
- 2024-09-23 从零开始学Python:第十一课-常用数据结构之列表
- 2024-09-23 苹果鱼教你入门Python(二):列表(列表 python)
- 2024-09-23 Python3 列表详解(python列表入门)
- 2024-09-23 DAY4-step6 Python示例说明range()函数:浮点数,列表,For循环
- 2024-09-23 每天三分钟一起学python之(六)python数据结构——列表
- 2024-09-23 python——列表的使用(python中列表常用方法)
- 2024-09-23 Python小案例38-列表的定义和创建
- 2024-09-23 Python中列表(list(python中列表list索引的用法)
- 2024-09-23 Python基础之列表(python列表教程)
- 1514℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 573℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 513℃MySQL service启动脚本浅析(r12笔记第59天)
- 486℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 486℃启用MySQL查询缓存(mysql8.0查询缓存)
- 469℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 449℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 446℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (83)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- c语言min函数头文件 (68)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)