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

liftword6个月前 (12-19)技术文章98


遍历整个列表:

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 列表的遍历和循环 python列表的遍历输出

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

一文了解 Python 列表 python列表常用的五种方法

假设你打算去附近的商店购买必需品,你要做的第一件事是什么?有答案了吗?是的,你可能会写下购物清单!Python 有一个名为 list 的内置数据结构,它与你的购物清单非常相似。这篇文章介绍 Pytho...

Python编程之第9节(循环控制 for)

1,循环,即循环执行同一块代码体,与其他语言程序不同的是,Python是通过遍历序列对象(如元组、列表、字典等)来循环(从第1个遍列到最后1个)2,语法3,break语句:用于中断for循环,即退出f...

Python编程:迭代器协议与遍历,轻松搞定

前言前篇内容的介绍应该能很容易地让我们理解掌握Python中的可迭代对象和迭代器(Iterable & Iterator)。本次内容我们来进一步介绍迭代器的有关内容。并请各位读者朋友们记得 点赞、转发...