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

liftword3周前 (12-19)技术文章19


遍历整个列表:

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 循环遍历列表使用 for 循环在列表中进行迭代是实现遍历列表的最简单和最基本的方法。「语法...

开眼界!Python遍历文件可以这样做

来源:【公众号】Python技术Python 对于文件夹或者文件的遍历一般有两种操作方法,一种是至二级利用其封装好的 walk 方法操作: import os for root,d...

python 列表的遍历和循环 python列表的遍历输出

在Python中,列表是一种常用的数据结构,用于存储一组有序的元素。列表的遍历和循环是常见的操作,可以通过多种方式实现。1. 使用for循环遍历列表:my_list = [1, 2, 3, 4, 5]...

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

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

Python列表的使用方法 python列表的常用方法

理解 Python 列表从本质上讲,列表是项目的集合,每个项目都由一个索引标识。列表的用途非常广泛,可以存储各种数据类型,包括数字、字符串,甚至其他列表。它们是 Python 的基本数据结构之一,广泛...

Python 中的列表推导式详解 python列表讲解

· 在 Python 中,列表推导式是一种简洁而富有表现力的方式,通过以紧凑的语法指定元素和条件来创建数据结构,如列表、字典和集合,通常使用单行代码,而不是使用传统的循环和附加操作。 在 Python...