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

教育分類資訊推薦

招考專家說|南京市金陵中學:遼闊金中,讓優秀者更優秀 - 天天要聞

招考專家說|南京市金陵中學:遼闊金中,讓優秀者更優秀

2025年中考志願填報在即,親愛的考生和家長,此刻的你們可能有一些迷茫,該選擇哪所學校?日前,現代快報教育中心推出「2025 AI上名校·招考專家說」直播訪談,邀請招考專家走進直播室,解讀學校教育課程,點撥志願填報技巧,帶你一起上名校!
重慶移通學院學生會當選中華全國學生聯合會代表團體出席會議 - 天天要聞

重慶移通學院學生會當選中華全國學生聯合會代表團體出席會議

日前,中華全國青年聯合會第十四屆委員會全體會議、中華全國學生聯合會第二十八次代表大會在北京隆重召開。重慶移通學院學生會當選為中華全國學生聯合會第二十八次代表大會正式代表團體,學校學生會執行主席何杰同學代表重慶移通學院學生會,作為全國學聯第二
浦東小學生愛心暑托班開班,各辦班點化身「夏日充能站」 - 天天要聞

浦東小學生愛心暑托班開班,各辦班點化身「夏日充能站」

7月7日一早,位於浦東金楊新村街道的香山中學迎來了愛心暑托班的孩子們,大家正在聚精會神地聆聽開班第一課《高舉隊旗跟黨走 爭當科創小先鋒》,教育引領少先隊員們爭當愛黨愛國、勤奮好學、全面發展的新時代好少年。記者在現場看到,各個教室里都開展着豐富的暑托班活動,這一邊的班主任帶領孩子們認真學習夏季防暑等實用...
十餘所「雙一流」高校,密集成立新學院! - 天天要聞

十餘所「雙一流」高校,密集成立新學院!

近期,武漢大學、西安交通大學、上海交通大學、復旦大學、北京師範大學、北京理工大學、北京航空航天大學、中國傳媒大學等十餘所「雙一流」高校官宣成立新學院,聚焦人工智能、未來技術、空天技術、量子科技、AI....
曲靖經開區卓然學校2025年教師崗位招聘公告(補招) - 天天要聞

曲靖經開區卓然學校2025年教師崗位招聘公告(補招)

曲靖經開區卓然學校2025年教師崗位招聘公告(補招)一、學校簡介曲靖經開區卓然學校(原北附曲靖經開區實驗學校),2022 年6月,依據曲靖市規範民辦義務教育發展要求,北附曲靖經開區實驗學校另行選址與曲靖經濟技術開發區卓然學校合併辦學成立了曲
共築保密防線  維護國家安全 - 天天要聞

共築保密防線 維護國家安全

濱州日報/濱州網訊6月30日上午,正值國家安全法頒佈十周年,也是新修訂保密法實施一周年之際。市殘聯組織黨員幹部赴總體安全觀濱州市教育實訓基地暨保密教育實訓平台,開展「共築保密防線 維護國家安全」保密宣傳教育暨主題黨日活動。
「法X功」學校舞蹈教師病亡,年僅33歲 - 天天要聞

「法X功」學校舞蹈教師病亡,年僅33歲

自中國反邪教網發佈《李洪志及境外「法X功」邪教組織醜行劣跡線索徵集》以來,不斷收到網友提供的線索。▲安妮·索恩訃告網友提供,2025年5月7日,「法X功」旗下「飛天藝術學院」加州分校項目總監、舞蹈老師安妮·索恩(Anne Sohn)因癌症病
高考後現「醫美」熱潮,學生扎堆整形!這些風險要注意 - 天天要聞

高考後現「醫美」熱潮,學生扎堆整形!這些風險要注意

近幾年,每年高考後都會出現一陣「醫美」熱潮,「雙眼皮手術爆單」「打瘦臉針要搶號」……不少高中畢業生趁暑假做醫美,準備「改頭換面」迎接大學生活。未成年人適合做醫美手術嗎?這類手術背後,潛藏着哪些容易被忽視的風險?
學生管理——2026級新生家長會安排 - 天天要聞

學生管理——2026級新生家長會安排

一、目的:向家長宣傳我校辦學成就,介紹學校課堂教學改革指導思想、措施與成效,說明學生在校學校提供的各項服務保障工作讓家長放心,爭取家長對學校工作的理解與支持。二、時間、地點:報到第一天(**月**日)上午8:30開始,11:30結束。地點在
不出國門也能培養世界人才!多所高校共議「在地國際化」之路 - 天天要聞

不出國門也能培養世界人才!多所高校共議「在地國際化」之路

如何讓中國學子不出國門就能享受世界一流名校的優質教育資源?這不僅是中外合作大學的課題,更是所有類型高校共同關注的方向。2025年「新時代高等教育在地國際化與青年人才培養」國際研討會於7月2日至4日在寧波諾丁漢大學舉行。本次論壇聚焦「在地國際化」與「青年人才培養」,涵蓋開幕式暨主論壇,兩大垂直領域分論壇和一...