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

教育分类资讯推荐

实景教学!孩子公园学这个,关键时能救命! - 天天要闻

实景教学!孩子公园学这个,关键时能救命!

为加强防溺水安全教育,提升市民自我保护意识和应急避险能力,7月4日,洪山区城市管理执法局在南湖幸福湾公园举办了一场主题为“同心防溺水,安全共守护”的“沉浸式”安全教育活动,通过情景演练、家园联动等形式,让安全知识入脑入心。活动伊始,洪山区公园管理中心工作人员结合园区环境,开展防溺水应急演练。围绕预防溺...
安徽财贸职业学院举行2025届毕业典礼 - 天天要闻

安徽财贸职业学院举行2025届毕业典礼

7月5日上午,安徽财贸职业学院在翡翠校区图书馆报告厅举行2025届毕业典礼。学院党委书记张志祥主持,学院领导吴成颂、何翔、胡戴新、罗虹、王海龙出席典礼,学院各部门、二级院部党政主要负责人及毕业班辅导员代表、优秀毕业生代表参加仪式。
网友篡改伪造高中录取分数线,拘留! - 天天要闻

网友篡改伪造高中录取分数线,拘留!

近日,昭通市教育体育局发现某短视频账号“云南中考升学王老实”将《昭通市2024年普通高中第一批次录取分数》恶意篡改伪造为2025年版本在短视频平台进行传播。昭通市教育体育局及时向公安机关报案,造谣者已被公安机关查获,依法处以行政拘留10日。昭通市教育体育局严正声明:网传“昭通市2025年普通高中第一批次录取分数”...
祝贺!高考录取通知书来了→ - 天天要闻

祝贺!高考录取通知书来了→

@高考考生你的录取通知书可能已在路上!7月3日起2025年高考录取通知书EMS邮件查询通道正式开通!亲爱的同学快来看看你的通知书到哪里了?录取通知书物流这里查!
招考专家说|南京市金陵中学:辽阔金中,让优秀者更优秀 - 天天要闻

招考专家说|南京市金陵中学:辽阔金中,让优秀者更优秀

2025年中考志愿填报在即,亲爱的考生和家长,此刻的你们可能有一些迷茫,该选择哪所学校?日前,现代快报教育中心推出“2025 AI上名校·招考专家说”直播访谈,邀请招考专家走进直播室,解读学校教育课程,点拨志愿填报技巧,带你一起上名校!
重庆移通学院学生会当选中华全国学生联合会代表团体出席会议 - 天天要闻

重庆移通学院学生会当选中华全国学生联合会代表团体出席会议

日前,中华全国青年联合会第十四届委员会全体会议、中华全国学生联合会第二十八次代表大会在北京隆重召开。重庆移通学院学生会当选为中华全国学生联合会第二十八次代表大会正式代表团体,学校学生会执行主席何杰同学代表重庆移通学院学生会,作为全国学联第二