当前位置: 首页 > news >正文

python---数据类型(列表)

组织列表
使用sort()方法对列表永久性排序

按照字母顺序排序:

motorcycles = ['chunlan', 'yamaha', 'dayun', 'jianshe']motorcycles.sort()print(motorcycles)

字母倒序:

motorcycles = ['chunlan', 'yamaha', 'dayun', 'jianshe']motorcycles.sort(reverse=True)print(motorcycles)
使用sorted()函数进行临时排序
motorcycles = ['chunlan', 'yamaha', 'dayun', 'jianshe']print("Here is the original list:")
print(motorcycles)print("\nHere is the sorted list:")
print(sorted(motorcycles))print("\nHere is the original list again:")
print(motorcycles)

也可以向sorted()传递参数reverse=True倒序排列:

motorcycles = ['chunlan', 'yamaha', 'dayun', 'jianshe']print("Here is the original list:")
print(motorcycles)print("\nHere is the sorted list:")
print(sorted(motorcycles, reverse=True))print("\nHere is the original list again:")
print(motorcycles)
倒着打印列表

不是倒序,是倒着,该方法也是永久性地颠倒列表的顺序,不过可以再次调用,恢复之前的顺序:

motorcycles = ['chunlan', 'yamaha', 'dayun', 'jianshe']
print(motorcycles)
motorcycles.reverse()print(motorcycles)motorcycles.reverse()
print(motorcycles)
确定列表的长度

使用len()函数

motorcycles = ['chunlan', 'yamaha', 'dayun', 'jianshe']
print(len(motorcycles))
操作列表
遍历列表

使用for循环:

magicians = ['alice', 'david', 'carolina', 'john', 'kite']
for magician in magicians:print(magician)
深入循环

使用缩进表示语句属于for的循环体:

magicians = ['alice', 'david', 'carolina', 'john', 'kite']
for magician in magicians:print(magician.title() + ", that was a great trick!")print("I can't wait to see your next trick, " + magician.title() + ".\n")

也可以在循环结束后执行操作,只需要不再缩进即可:

magicians = ['alice', 'david', 'carolina', 'john', 'kite']
for magician in magicians:print(magician.title() + ", that was a great trick!")print("I can't wait to see your next trick, " + magician.title() + ".\n")print("Thank you, everyone. That was a great magic show!")
创建数值列表
使用函数range()

打印1-4:

for value in range(1, 5):print(value)
使用range()创建数字列表

使用list()函数将range()的结果直接转换为列表:

numbers = list(range(1,6))
print(numbers)even_numbers = list(range(2,11,2))  #最后一个2是步长
print(even_numbers)

创建平方的列表:

squares = []
for value in range(1,11):square = value**2squares.append(square)print(squares)

也可以简写为:

squares = []
for value in range(1,11):squares.append(value**2)print(squares)
对数字列表执行简单的统计
digits = [1,2,3,4,5,6,7,8,9,0]
print(min(digits))
print(max(digits))
print(sum(digits))
列表解析

列表解析将for循环和创建新的元素代码合成一行,并自动附加新元素。了解一下

squares = [value**2 for value in range(1,11)]
print(squares)
使用列表的一部分
切片

要创建切片,可指定要使用的第一个元素和最后一个元素的索引。与range()函数一样,python在到达指定的第二个索引前面的元素停止。range(1,10)

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
print(players[1:4])
print(players[:3])
print(players[2:])
print(players[-3:])
遍历切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print("Here are the first three players on my team:")
for player in players[:3]:print(player.title())

复制列表

players = ['charles', 'martina', 'michael', 'florence', 'eli']
ourplayers = players[:]
print("Players are:")
print(players)
print("Ourplayers are:")
print(ourplayers)

所谓的列表,就是一个序列,其中的元素有序,可变,不限类型

http://www.lryc.cn/news/216347.html

相关文章:

  • CentOS 7升级gcc/G++版本
  • uni-app开发微信公众号 H5打开扫一扫功能
  • k8s-服务网格实战-入门Istio
  • 金属压铸件自动化3D全尺寸测量设备自动外观检测三维检测-CASAIM
  • Android笔记(十):结合Navigation组件实现Compose界面的导航
  • linux内核tcp配置--断网后连接卡住
  • Apache Pulsar 在腾讯云上的最佳实践
  • VMware 虚拟机安装 CentOS 7
  • UnityAI——个体AI角色的操控行为脚本
  • ssh登录界面变成vim提示,进不去系统
  • 虹科示波器 | 汽车免拆检修 | 2012 款上汽大众帕萨特车 发动机偶尔无法起动
  • UE5.0.3版本 像素流送 Pixel Streaming
  • BetterDisplay Pro v1.4.15(显示器管理管理软件)
  • 蓝桥等考C++组别四级006
  • app开发之后需要做什么
  • 某汽车金融企业:搭建SDLC安全体系,打造智慧金融服务样本
  • iOS GCD(Grand Central Dispatch)
  • Cross-Entropy Loss(多分类损失函数)
  • TP858 3BSE018138R1 具有高性能CPU的工业PC技术
  • Observability:使用 OpenTelemetry 手动检测 .NET 应用程序
  • 生产事故:redis主从的坑
  • maven本地仓库有依赖包,还会远程下载的问题
  • 动作捕捉系统处理单点多点丢点问题
  • FIFO 位宽转换
  • 瑞明达:聚“追梦”之力,共圆“经济梦”
  • UE5数字孪生制作(一) - QGIS 学习笔记
  • STM32 使用HAL库,HAL_Delay()会卡死, 程序一直卡在 HAL_GetTick( ) 函数中(已解决)
  • Maven Repository使用
  • 智安网络|保护您的应用程序免受攻击:重要的安全强化措施
  • python3.8 use async getting invalid sysntax