掌握 Python 列表:您需要了解的基本技术

霞舞
发布: 2024-11-07 17:21:02
转载
1167人浏览过

掌握 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 来检查是否到达末尾。

表单大师AI
表单大师AI

一款基于自然语言处理技术的智能在线表单创建工具,可以帮助用户快速、高效地生成各类专业表单。

表单大师AI 74
查看详情 表单大师AI
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 列表:您需要了解的基本技术的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号