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

liftword4个月前 (12-19)技术文章60


遍历整个列表:

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有多种方法可以用于遍历指定目录中的内容,其中一种常用的方法是使用os模块的walk()函数。os.walk()函数可以遍历一个目录以及所有的子目录,并且对于每一个目录,都可以产生一个三元组...

Python数据分析实战-依次遍历dataframe行和列(源码和实现效果)

实现功能:Python实现dataframe遍历行和列实现代码:import pandas as pd df = pd.read_csv("G:\数据杂坛\datasets\kidney_d...

python入门027:while循环处理列表

一、while循环处理列表使用while循环可以在遍历列表的同时对其进行修改。1、在列表之间移动元素使用while循环,可以将一个列表中的元素提取出来,加入到另一个列表中。在上例中,我们首先创建了一个...

网络工程师如何使用 Python 批量收集网络设备的 MAC 地址?

在网络管理中,网络工程师经常需要收集和管理大量设备的MAC地址。这些MAC地址对于维护网络设备的安全性和稳定性至关重要。在大型网络环境中,手动收集这些信息既费时又容易出错。因此,利用Python脚本自...

Python数据分析学习笔记8——NumPy数组遍历与排序

数组遍历使用for循环来实现# -*- coding:utf-8 -*- import numpy as np print('一维数组') arr = np.array([10,20...