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分)等。