掌握 Python 列表:您需要了解的基本技术
来源:dev.to
2024-11-09 15:31:03
0浏览
收藏
哈喽!大家好,很高兴又见面了,我是golang学习网的一名作者,今天由我给大家带来一篇《掌握 Python 列表:您需要了解的基本技术》,本文主要会讲到等等知识点,希望大家一起学习进步,也欢迎大家关注、点赞、收藏、转发! 下面就一起来看看吧!

为了
简单的
这将循环遍历列表,并且列表中的每个元素在每次迭代中都可以作为变量使用。当需要遍历列表中的所有元素时,这被广泛使用。
operating_systems = ["windows", "mac", "linux"]
for os in operating_systems:
print(os)`
# output windows mac linux
对于和范围
当需要根据索引访问并且需要索引值时。
operating_systems = ["windows", "mac", "linux"]
for i in range(len(operating_systems)):
print(f"index {i}: {operating_systems[i]}")
# output index 0: windows index 1: mac index 2: linux
为并枚举
如果您同时需要索引和值,这是一种优雅的方式
operating_systems = ["windows", "mac", "linux"]
for index, os in enumerate(operating_systems):
print(f"index is {index} and value is {os}")
# output index is 0 and value is windows index is 1 and value is mac index is 2 and value is linux
尽管
简单的同时
operating_systems = ["windows", "mac", "linux"]
i = 0 # inital condition, required to start
while i < len(operating_systems):
print(f"while looping {i} got the value {operating_systems[i]}")
i = i + 1 # this is very important, dont forget about infinite loops
# output while looping 0 got the value windows while looping 1 got the value mac while looping 2 got the value linux
迭代器
可以很好地控制何时向前移动迭代器,尽管我们必须依靠 stopiteration 来检查是否到达末尾。
operating_systems = ["windows", "mac", "linux"]
iterator = iter(operating_systems)
while true:
try:
os = next(iterator)
print(f"consumed form iterator {os}")
except stopiteration:
print("consumed all from iterator")
break
# output consumed form iterator windows consumed form iterator mac consumed form iterator linux consumed all from iterator
# hack to avoid stopiteration
iterator = iter(operating_systems)
end_of_list = object()
reached_end = false
while not reached_end:
os = next(iterator, end_of_list)# a predefined object as end of the list
if os != end_of_list:
print(os)
else:
reached_end = true
列表理解
需要转型时
operating_systems = ["windows", "mac", "linux"] os_uppercase = [os.upper() for os in operating_systems] print(os_uppercase)
# output ['windows', 'mac', 'linux']
骑自行车
当需要循环浏览列表时。使用适当的边界条件来打破循环
import itertools
operating_systems = ["windows", "mac", "linux"]
for item in itertools.cycle(operating_systems):
print(item)
# infinite cycling loopmake sure to have proper boundary condition to break
# output windows mac linux windows mac linux windows mac linux windows mac linux windows ....... infinite loop
多个列表
同时循环多个列表。如果列表大小不同,请注意输出。
operating_systems = ["windows", "mac", "linux"]
mobile_operating_systems = ["android", "ios"]
for os, mobile_os in zip(operating_systems,mobile_operating_systems):
print(os, mobile_os)
# output windows android mac ios
反向循环
operating_systems = ["windows", "mac", "linux"]
for reversed_os in reversed(operating_systems):
print(reversed_os)
# Output linux mac windows
今天关于《掌握 Python 列表:您需要了解的基本技术》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
版本声明
本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
如何在飞机上使用笔记本电脑?
- 上一篇
- 如何在飞机上使用笔记本电脑?
- 下一篇
- NPM 对等依赖关系深入:全面介绍
查看更多
最新文章
-
- 文章 · python教程 | 1天前 | python · 进程管理 · Python subprocess Popen 进程树 TimeoutExpired 超时清理
- Python subprocess 超时后清理子进程树
- 108浏览 收藏
-
- 文章 · python教程 | 1天前 | 日志 · logging · Python教程 · Python contextvars request_id LogRecord logging.Filter
- Python logging.Filter 注入请求上下文的做法
- 410浏览 收藏
-
- 文章 · python教程 | 4天前 | Python教程 · Python 鸭子类型 typing.Protocol 结构子类型
- Python typing.Protocol 约束鸭子类型接口
- 246浏览 收藏
-
- 文章 · python教程 | 4天前 | Python教程 · Python 相对路径 is_file pathlib Path.resolve
- Python pathlib 相对路径规范化与文件判断
- 144浏览 收藏
-
- 文章 · python教程 | 4天前 | python · 异步编程 · asyncio · Python CancelledError asyncio.timeout TimeoutError
- Python asyncio.timeout 嵌套取消与异常传播
- 373浏览 收藏
-
- 文章 · python教程 | 5天前 |
- Python heapq 最大堆 API 怎么避免手动取负数
- 397浏览 收藏
-
- 文章 · python教程 | 5天前 | python · Python 不可变对象 namedtuple dataclass copy.replace
- Python copy.replace 怎么更新不可变对象字段
- 245浏览 收藏
-
- 文章 · python教程 | 5天前 | python ·
- Python NamedTemporaryFile 的 delete_on_close 怎么设置
- 311浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
查看更多
AI推荐
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 314次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 370次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 369次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 335次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 158次使用
查看更多
相关文章
-
- Python sqlite3 Connection serialize 怎么导出数据库快照:备份窗口、内存占用与恢复校验
- 2026-08-26 501浏览
-
- Python监控网页状态:requests异常处理实战
- 2026-05-29 501浏览
-
- TensorFlow模型部署为API的TF Serving方法
- 2026-05-26 501浏览
-
- Python字符串编码转换:encode与decode详解
- 2026-05-16 501浏览
-
- TensorFlow裁剪无用算子方法详解
- 2026-05-15 501浏览

