Python基础: 列表遍历

2024年12月06日07:23:14 教育 1745


Python基础: 列表遍历 - 天天要闻

遍历整个列表:

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 添加冒号。

教育分类资讯推荐

花几大千找高报师填志愿你觉得值吗? - 天天要闻

花几大千找高报师填志愿你觉得值吗?

当今世界,还是有很多家长花几大千甚至上万(某网红志愿填报公司动不动一个名额1万多)去找高报师给自己孩子填志愿,你们觉得真的值得花这个钱吗?还是智商税?
​2025年高考有何特殊之处 - 天天要闻

​2025年高考有何特殊之处

视觉中国供图 7月7日,距离2025年高考刚好过去了一个月。 对教育领域来说,每年夏天最大的事就是高考了。作为一个报道教育多年的记者,因其典型的“风向标”作用,我关注最多、研究最多的也是高考——向下,它影响着12年基础教育的教与学;向上,又直接关系到高等教育的质量。 今年的高考有些“特别”,甚至有人说今年的高...
偷录手机通话能作呈堂证供吗? - 天天要闻

偷录手机通话能作呈堂证供吗?

苹果手机系统升级,增加了支持通话录音功能,引发广大网友的关注。在当前的民事诉讼中,有越来越多的当事人将通话录音作为证据提交法院。那么,通话录音能否作为证据?偷录的通话录音是否具有合法性?符合证据要求的通话录音又该如何采集?(图源网络 侵删)通话录音可否作为证据向法院提交?如今,通过手机打电话、发信息...
2025四川省专科大学名单!附2024年录取分数线参考 - 天天要闻

2025四川省专科大学名单!附2024年录取分数线参考

四川专科学校包括四川交通职业技术学院、成都航空职业技术学院、四川铁道职业学院等。2024年四川专科公办学校分数较低的有成都工业职业技术学院(409分)、绵阳职业技术学院(411分)、四川航天职业技术学院(412分)等。