Python基础: 列表遍历 python怎样遍历列表中数字

liftword5个月前 (12-19)技术文章73


遍历整个列表:

For 循环:

  • 使用列表项自动执行重复性任务。
  • 示例: for magician in magicians: print(magician) .
magicians = ['alice', 'david', 'carolina']
 for magician in magicians:
    print(magician)

>>

alice
 david
 carolina

迭代所有项:

  • Loop 检索并处理每个项目。
  • 示例:在列表中打印每个魔术师的名字。
magicians = ['alice', 'david', 'carolina']
 for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")

>>

 Alice, that was a great trick!
 David, that was a great trick!
 Carolina, that was a great trick!

循环执行:

  • 对每个列表项重复上述步骤。
  • 示例:使用循环的个性化消息。
magicians = ['alice', 'david', 'carolina']
 for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")
    print(f"I can't wait to see your next trick, {magician.title()}.\n")

>>

Alice, that was a great trick!
I can't wait to see your next trick, Alice.

David, that was a great trick!
I can't wait to see your next trick, David.

Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.

循环后操作:

  • 循环后的代码执行一次。
  • 示例:在单独消息后感谢所有魔术师。
magicians = ['alice', 'david', 'carolina']
 for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")
    print(f"I can't wait to see your next trick, {magician.title()}.\n")
 print("Thank you, everyone. That was a great magic show!")


>>

Alice, that was a great trick!
 I can't wait to see your next trick, Alice.

 David, that was a great trick!
I can't wait to see your next trick, David.

 Carolina, that was a great trick!
 I can't wait to see your next trick, Carolina.

 Thank you, everyone. That was a great magic show!

避免 Python 中的缩进错误

忘记缩进:

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)  # Should be indented

#Error : 
File "magicians.py", line 3
    print(magician)
    ^
 IndentationError: expected an indented block after 'for' statement on line 2
  • 错误: IndentationError: expected an indented block after 'for' statement
  • 修复:缩进 print(magician) 行。

忘记缩进额外的行:

for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")  # Should be indented


#OutPut

Alice, that was a great trick!
 David, that was a great trick!
 Carolina, that was a great trick!
 I can't wait to see your next trick, Carolina.
  • 问题:只有最后一个魔术师会收到第二条消息,因为该行不在循环内。
  • 修复:缩进第二个 print 语句。

不必要地缩进:

message = "Hello Python world!"
    print(message)  # Unnecessary indent

#Error : 

File "hello_world.py", line 2
    print(message)
    ^
 IndentationError: unexpected indent
  • 错误: IndentationError: unexpected indent
  • 修复:删除不必要的缩进。

在循环后缩进:

for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")
    print(f"I can't wait to see your next trick, {magician.title()}.\n")
    print("Thank you everyone, that was a great magic show!")  # Should not be indented


>>

Alice, that was a great trick!
I can't wait to see your next trick, Alice.
Thank you everyone, that was a great magic show!

 David, that was a great trick!
 I can't wait to see your next trick, David.
 Thank you everyone, that was a great magic show!

 Carolina, that was a great trick!
 I can't wait to see your next trick, Carolina.
 Thank you everyone, that was a great magic show
  • 问题:感谢信息是为每位魔术师打印的。
  • 修复:取消缩进最后一个 print 语句。

忘记冒号:

for magician in magicians  # Missing colon
    print(magician)


#Error : 

 File "magicians.py", line 2
    for magician in magicians
                             ^
 SyntaxError: expected ':'
  • 错误:语法错误:应为“:”
  • 修复:magicians 中为 magician 添加冒号。

相关文章

一文掌握Python 字典遍历的8种方法

Python 中的字典是键值对的集合,其中每个键都是唯一的。它们广泛用于各种用途,例如存储配置设置、管理数据或映射数据关系。为了有效地使用字典,需要了解如何循环访问它们1. 使用 For 循环遍历字典...

python列表的长度及遍历 python中列表长度的函数

在 Python 中,可以使用 len() 函数来获取一个列表的长度,并使用 [] 运算符来创建一个列表。例如:python# 创建一个列表my_list = [1, 2, 3, 4, 5]# 访问列...

python笔记2-遍历文件夹目录os.walk()

如何遍历查找出某个文件夹内所有的子文件呢?并且找出某个后缀的所有文件walk功能简介os.walk() 方法可以用于在文件目录中进行查找和遍历操作。os.walk() 方法是一个简单易用的文件、目录遍...

Python文件遍历之os、pathlib 遍历文件夹python

20221208星期四:# os方式一:通过递归调用,拿到所有文件,或者指定结尾的文件:# os方式二:通过 os.walk ,拿到目录下所有的目录及文件:# pathlib方式三:通过 rglob...

从零开始学习python(5)——分支与循环结构

1. 分支结构Python中的分支结构主要是由if语句实现的,一共有四种。1.1 if语句判断一个条件,如果这个条件成立,就执行其包含的某条语句或某个代码块。语法规则:if condition:sta...

你想不到的,那些在 Python 中输出列表的技巧

知道有很多方法可以在 Python 中输出列表吗?本文将探讨在 Python 中输出列表的一些技巧。Python 中的 print() 函数是大家经常使用的内置函数,可以方便的输出列表,但下面列举的使...