首页 > 后端开发 > Python教程 > 掌握 Python 列表:您需要了解的基本技术

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

Patricia Arquette
发布: 2024-11-12 22:28:02
原创
240 人浏览过

Mastering Python Lists: Essential Techniques You Need to Know

为了

简单的

这将循环遍历列表,并且列表中的每个元素在每次迭代中都可以作为变量使用。当需要遍历列表中的所有元素时,这被广泛使用。

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 列表:您需要了解的基本技术的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板