
2025 HKDSE ICT Programming Syllabus 中三課程清Concept Python Programming List 串列配合FOR Loop 使用 List Comprehension 串列綜合運算/列表推導 ... ... <看更多>
Search
2025 HKDSE ICT Programming Syllabus 中三課程清Concept Python Programming List 串列配合FOR Loop 使用 List Comprehension 串列綜合運算/列表推導 ... ... <看更多>
python list comprehension用法 2022-在Mobile01/PTT/Yahoo上的房地產討論內容懶人包,找list comprehension教學,list comprehension中文,python list comprehension ... ... <看更多>
python list comprehension用法 2022-在Mobile01/PTT/Yahoo上的房地產討論內容懶人包,找list comprehension教學,list comprehension中文,python list comprehension ... ... <看更多>
Python 初學觀念陷阱卡29-list comprehension 】 透過之前一系列對於Python 資料型態與迴圈的介紹相信大家愈來愈熟悉 ... 接著來看看list comprehension 的常見用法: ... <看更多>
在Python 中,我們應該盡量避免: 用index 來循環list; 用key 來循環dictionary. 不是說不可以,只是有更簡明的寫法,上述兩種用法的缺點都在於,在循環內我們必須多做 ... ... <看更多>
寫過Python 的人大概都知道,在複製list 的時候最好不要直接指定,而要使用 copy 函式,但可能有些時候,我們還是會不小心觸發這個黑魔法,所以今天 ... ... <看更多>
... <看更多>
#1. [Python教學]Python Comprehension語法應用教學
透過今天的教學,您將可以學到Python Comprehension如何應用在以下幾個重要的物件(Object)上,並且有能力將現有程式碼改寫為Python Comprehension語法。 List ...
#2. List Comprehension: Python 的For Loop 怎樣使用?
List Comprehension 更進一層樓的用法是加入條件式處理。這個使List Comprehension 涵蓋更為廣泛的應用。 當中可以分為2 種用法:篩選式用途(Filtering) ...
#3. list comprehension 使用方法(內含範例程式碼) sample code
list comprehension 是一個能夠以一行處理掉list 邏輯的python 程式碼, 通常使用時,可以更節省空間,並且縮短程式碼的行數 (相反的,也可能減少了程式碼易讀性) ...
#4. Python 基礎語法教學Part 4 - iT 邦幫忙
何謂List Comprehension. 說老實話,要我翻譯這個字,我還真的不會XD。 這個功能是以一個特別的方式應用for迴圈,並創造出帶有元素的List。 應用List Comprehension, ...
#5. Python – List Comprehension - Benjr.tw
List Comprehension 語法可以依據需求快速建立list ,會使用到lambda 匿名函 ... 到新的list 物件squares. range 型別的用法– https://benjr.tw/104527
#6. Python—list comprehension列表解析使用方法 - CSDN博客
Python —list comprehension列表解析使用方法. DgLink 于 2022-01-08 16:11:55 发布 871 收藏 5. 文章标签: python list. 版权声明:本文为博主原创文章,遵循 CC 4.0 ...
#7. 5. 資料結構— Python 3.11.3 說明文件
你可以使用方法 append() 將一個項目放到堆疊的頂層。而使用方法 pop() 且不給定索引值去取得 ... List comprehension(串列綜合運算)讓你可以用簡潔的方法創建list。
#8. Python Comprehension語法應用教學 - 快樂學程式
Comprehension即是Python的Pythonic語法之一,可以運用在任何可疊代的 ... 集合(Set)Comprehension的用法和串列(List)Comprehension幾乎一樣,不同的 ...
#9. Python 知识点: 列表推导式(list comprehension)
Python 里面有个很棒的语法糖(syntactic sugar),它就是list comprehension ,有人把它翻译成“列表推导式”,也有人翻译成“列表解析式”。
#10. 【Python】List 串列配合FOR Loop 使用List ... - YouTube
2025 HKDSE ICT Programming Syllabus 中三課程清Concept Python Programming List 串列配合FOR Loop 使用 List Comprehension 串列綜合運算/列表推導 ...
#11. list comprehension @ python note :: 隨意窩Xuite日誌
另外在list comprehension還有個有趣的用法,就是可以改變原本list內的資料內容, 而且一次可以賦兩個值做修改,這方法在把複雜的list做元素提取時很有用,舉例來說:.
#12. List Comprehension - HackMD
tags: Python_30 R & python. List Comprehension; 清單(List)的Comprehension. 結合if條件式. 全部文章列表. Python語法簡單易學且非常簡潔
#13. Python Comprehension語法應用教學(上) - 104學習精靈
Q:Python List Comprehension如何使用? 串列(List)Comprehension的語法包含三個部分,說明如下: [expression for item in iterab...
#14. [Python]List Comprehensions. List… | 宅男雜誌 - Medium
“[Python]List Comprehensions” is published by Young Chen in 宅男雜誌. ... 除了簡潔之外,List Comprehension 這種比較Pythonic的撰寫模式有效率 ...
#15. List comprehension - 知乎专栏
但在用python写程序的时候,突然滚入了pythonic的技巧。是的,list comprehension ,挺有意思的。发现网上部分翻译为列表解析,部分翻译为列表推导…
#16. yield with list comprehension - wdv4758h-notes - Read the Docs
之前無意間發現可在 list comprehension 裡面使用 yield 和 yield from , 這是一個 tricky 的用法 :P. Example¶. list : >>> list((yield x) ...
#17. python list comprehension用法2022-在Mobile01/PTT/Yahoo上的 ...
python list comprehension用法 2022-在Mobile01/PTT/Yahoo上的房地產討論內容懶人包,找list comprehension教學,list comprehension中文,python list comprehension ...
#18. list comprehension python - 稀土掘金
List Comprehension 不仅可以用于列表,还可以用于其他可迭代对象,如元组、字典和集合等。它还支持嵌套和多个条件过滤等高级用法,因此是Python中一个非常强大和灵活的 ...
#19. 不間斷Python 挑戰Day 10 - 進階for 迴圈 - 方格子
另個for迴圈的應用是將串列和for迴圈結合,使用迭代的方式生成串列(list comprehension),讓程式的可讀性更高也更精簡。
#20. ccClub Python讀書會, profile picture - Facebook
Python 初學觀念陷阱卡29-list comprehension 】 透過之前一系列對於Python 資料型態與迴圈的介紹相信大家愈來愈熟悉 ... 接著來看看list comprehension 的常見用法:
#21. PowerPoint 簡報
各資料結構的操作; list comprehension. 建立完整的Python 程式. 主程式(main); 函式(function); Save XXX.py. 3. 題目. Write a program that lets the user perform ...
#22. 7 For 迴圈 - Hello Py: Python 程式設計
7.2 List Comprehension. 將事情變得更簡單一些:. starrings = ["Jennifer Aniston", "Courteney Cox", "Lisa Kudrow", "Matt LeBlanc", "Matthew Perry", ...
#23. Python 學習筆記: 串列, 字典, 與集合的生成式 ... - 小狐狸事務所
Python 的串列生成式(list comprehension) 是有別於其他語言的語法, 它可使程式碼更簡潔與具有Python ... Python 學習筆記: 內建函式zip() 的用法.
#24. Python 列表推導中的if...else | D棧 - Delft Stack
本教程演示瞭如何在Python 中使用if...else 語句進行列表推導。 ... Python Python List. Python 列表推導中的if...else. 本教程將解釋在Python 中 ...
#25. [Python]串列綜合表達式List Comprehension - Program – C.Y.C
本文從MATLAB 語法的角度來探討Python 中串列綜合表達式(List Comprehension…
#26. Python 3 Tutorial 第四堂(2)略談函數式程式設計
Python 是個支援多重典範的語言,雖然不鼓勵,不過想在Python 中進行一些函數式程式 ... 包含式其實概念是來自函數式程式設計中的List 包含式(List comprehension), ...
#27. python中lambda函数list comprehension 和zip函数使用指南
这篇文章主要介绍了python中lambda函数list comprehension 和zip函数使用方法,非常的实用,有需要的朋友可以参考下.
#28. python列表高级操作——快来看看你会多少! - 伙伴云
(1)列表生成式(高级用法)列表推导式(List Comprehension)是一种简化代码的优美方法。官方文档——**列表推导式提供了一种创建列表的简洁方法。
#29. Python的遞推式構造列表(List comprehension) - 台部落
python 語言的一種重要原則(zen)就是簡潔、自然,遞推式構造列表(List)、字典(dict)就是一個很好的例子。 我們的代碼在初始化一個List或者dict時經常 ...
#30. 11个技巧让你编写出更好的Python代码 - 51CTO
list comprehension 能力非常强大,甚至包括if语句。注意,list comprehension的用法有一点争议。它不应该被过度使用,尤其是当它损害了代码的可读性时 ...
#31. Python-QA/list使用append最後為什麼會把前面的元素都修改掉 ...
在Python 中,我們應該盡量避免: 用index 來循環list; 用key 來循環dictionary. 不是說不可以,只是有更簡明的寫法,上述兩種用法的缺點都在於,在循環內我們必須多做 ...
#32. 列表解析式(List Comprehension) - 我听过- 博客园
1、列表解析List Comprehension 举例:生成一个列表,元素0~9,对每一个元素自增1 ... 列表解析式的中括号换成大括号{}就行了立即返回一个集合用法:.
#33. Pythonic 實踐:實用的python 慣用法整理 - Mr. Opengate
Pythonic Code 的意思是:具有Python 特色的Python 程式碼,白話一點的說法 ... 下面列舉一些非常常見的List comprehension 用法與注意事項。
#34. Python的几个特性高效指令, since 2022-04-07 - 简书
(2022.04.07 Thu)用了下面方法,你的代码更Pythonic。 列表解析list comprehension 以紧凑的方式对list/tuple/dict的一系列...
#35. Python最常用的函数、基础语句有哪些? - 新浪
在python中enumerate的用法多用于在for循环中得到计数 ... Create a list of characters using list comprehension.
#36. 为什么tuple comprehension 生成一个generator - Julia中文社区
这个语法和Python 很像,Python 中 [i for i in (1, 2, 3)] 叫列表推导式(list comprehension),返回一个list。 而 (i for i in (1, 2, 3)) 叫生成器 ...
#37. Python 101 基礎教學(7) - Collections:list、tuple - June Monster
Python 的list跟其他語言的list或array比起來有著相當強大的優勢,有 ... 一些基礎用法,list/tuple還有非常多的觀念及用法,像是list comprehension ...
#38. [Python] 關於變數與參考的二三事- skyblog
寫過Python 的人大概都知道,在複製list 的時候最好不要直接指定,而要使用 copy 函式,但可能有些時候,我們還是會不小心觸發這個黑魔法,所以今天 ...
#39. Python 进阶之路(八) 最用心的推导式详解(附简单实战及源码)
列表推导式(list comprehensions); 字典推导式(dict comprehensions); 集合推导式(set comprehensions). 使用推导式可以简化代码的同时提高效率,在 ...
#40. Re: [問題] list的iterator應用問題- 看板Python - 批踢踢實業坊
原po的程式碼可以說是generator版本的list comprehension, ... 2 1 : 不好意思看了很久想不出來程式碼第一行跟結果的關係QQ : 這種用法是什麼意思呢?
#41. 手把手教你,53 个Python 经典面试题详解 - 墨天轮
Range函数可以接受1到3个参数,参数必须是整数。 请注意:我已经将range的每种用法包装在一个递推式构造列表(list comprehension)中 ...
#42. python入门——list列表-哔哩哔哩 - bilibili
https://www.w3schools.com/ python /python_lists.asp. ... python 小技巧七:列表解析式( list comprehension ) ... 24- Python 序列-list常见 用法.
#43. Python Programming - Weebly
>Python 課程002 -016 : List Comprehension 列表推導式 >Python 課程002 -017 : Functions 函數 ... conda 是一種很強大的工具,具體用法可以參照它的
#44. Python any() 和all() 函数 - freeCodeCamp
在本教程中,我们将了解Python 的any() 和all() 函数,并使用简单的示例来 ... 列表推导式(list comprehension)对于在列表中收集所有这些真假值非常 ...
#45. Python 高级特性· Linux 运维与DevOps 实战 - M-zhoujie2
Python 语言中有很多高级的用法,这些用法比较难理解,但熟悉后可以极大的提高编程的 ... 列表解析(list comprehension),也称为列表推导,是从Python 2.0 就被添加 ...
#46. 为什么列表理解在Python 中被这样称呼? - 七牛云
python. naming-conventions. list-comprehension. 发布于 -0800-12-31. 5 个回答 ... 如果我知道这个用法是通过那里进入数学史的,我也不会感到惊讶。
#47. 问答- 腾讯云开发者社区-腾讯云
python asynchronouslist-comprehensionpython-3.6. 我很难理解Python3.6中引入的异步理解的用法。作为免责声明,我在Python中处理异步代码的经验并不多。
#48. Paramiko functions python
Image by the Author. . append(squared) With a list comprehension, however, we. Mar 18, 2023 · 我正在使用Python Paramiko和scp在远程计算机上 ...
#49. 关于Python 列表推导的疑惑_w3cschool - 编程狮
关于“Python list comprehension 其实就是generator.”该怎么理解? ... 至于generator 的高级用法,比如模拟coroutine 什么的,可以暂时无视。
#50. Python如何使用List Comprehension? - srcmini
List Comprehension 被定义为在Python中定义, 创建列表的一种优雅方式, 它由方括号组成, 方括号包含一个表达式, 后跟for子句。
#51. List Comprehension and Generator Expression
前言. 本文将简单地介绍Python 中的列表推导式以及生成器表达式,用一些实例描述它们的基本使用方法,并 ...
#52. Python 函式zip() 教學:同時迭代多個list,學習刷題與資料分析 ...
同時迭代多個長短不一的list: zip() 的進階用法. Python zip() 每次從各個引數list 中各取一個元素配成同一組,彷彿拉鍊齒 ...
#53. 夠Python,一行指令輸出大心 - 智慧生活科技專業社群
... 文章"夠Python,利用集合持性來開發新版的XAXB小遊戲"在Python Taiwan社群中廣受各方高手的討論,讓敏哥成長很多,還學會了List comprehension, ...
#54. 列表推导(List Comprehension) - CodeAntenna
Python 列表推导教程(ListComprehensions)本文我们学习Python列表推导(listcomprehensions)、集合推导(setcomprehensions)以及字典推导(dictionarycomprehensions)。1.什么 ...
#55. `elif` in list comprehension conditionals - python - Stack Overflow
Python's conditional expressions were designed exactly for this sort of use-case: >>> l = [1, 2, 3, 4, 5] >>> ['yes' if v == 1 else 'no' if ...
#56. Python中那些神一樣的代碼,你知道嗎?不知道記得收藏!
這種表達式稱為list comprehension(列表推導) ... with的用法體現了python的一個精髓:把一些繁瑣的事務交給語言本身,開發者只要focus放在處理問題 ...
#57. Lambda to list python
WebbThe lambdas in the list comprehension are a closure over the scope of this ... 细说Python的lambda函数用法,建议收藏- 知乎- 知乎专栏Webb在Python中有两种 ...
#58. python--语句的多样性 - InfoQ 写作平台
... 赋值语句,条件语句,循环语句,列表推导式(list comprehension),pass,del ... for else的用法: for x in range(10): if(x==11): pass else: print('没有11的 ...
#59. 关于list comprehension 的翻译 - 水木社区
即使内涵的英文单词不这样的. 参考: 引自linuxforum -- python版 """ 关于两个术语的问题. [re: limodou]
#60. 翻译|Stack Overflow上关于Python的高票问答(一) - 文章详情
[1]不理解列表推导式(list comprehension)可以点击这里:Python 学习 ... Yield关键词的用法与return的用法几乎一致,他们只有一个区别: 当某个 ...
#61. 掌握Python列表理解需要知道的9件事,你知道嗎? - 人人焦點
list comprehension. [expression for item in iterable]# expanded form. for item in iterable: expression. 2.創建列表. 毋庸置疑,最流行的用法是 ...
#62. 列表推導式- 維基百科,自由的百科全書
列表推導式(list comprehension),是程序設計語言的一類語法結構,用於基於描述創建一個列表(list)資料 ... Python語言的列表推導式和生成器表達式的語法示例:.
#63. 列表生成式- 廖雪峰的官方网站
列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来 ... 使用列表生成式的时候,有些童鞋经常搞不清楚 if...else 的用法。
#64. 邏輯判斷( if、elif、else ) - Python 教學| STEAM 教育學習網
在現實世界裡,無時無刻都在進行著邏輯判斷,例如早餐要吃三明治還是麵包?飲料要喝珍奶還是咖啡?然而在Python 的世界裡,也提供了if、else、elif 三種語法來處理大量 ...
#65. Lis x*x for x in range 5
Python List Comprehension - TutorialsTeacher Websquares = [x*x for x in range(11)] ... python-for x in range的用法(注意要点、细节) - 脚本之家Web10 mei 2021 ...
#66. Python list 各要素计算
Python List (With Examples) - Programiz WebPython List Comprehension. ... 实例以下实例展示了count()函数的使用方法: #!/usr/bin/python aList = [123, ...
#67. Python list函数的用法
Python Lists are just like dynamically sized arrays, declared in other ... 内建函数)的用法- Python社区Web简单记一下python中List的sort方法(或者sorted内建 ...
#68. Python语句list range 1 10 3 执行结果为
以上就是python列表索引的两种用法,希望对大家有所帮助。 ... 2023 · 列表推导式(list comprehension)是指循环创建列表. for循环有非常广的应用场景,也可以用来创建 ...
#69. python 之选择结构( if- elif- else 绵绵羊博客园COJJAY
You can't use elif in list comprehension because it's not part of the ... 对python中的else和elif在if语句、for循环、while循环语句中的用法做了 ...
#70. Join list python int
List comprehension is a simple, easy-to-understand way to join a list of integers into a string ... python中map,join,int结合在一起的用法- CSDN博客Web27 jun.
#71. Cython list 型定義
WebCython can automatically convert many C types from and to Python types, ... list comprehension here to copy the C int values into a Python list of Python ...
#72. Python contains list function
putText()函数的用法 文章目录: 一、快速使用 二、官方文档 三、使用举例 虽然用啦 ... Using List Comprehension The most Pythonic solution is to use a list ...
#73. Lambda to list python
[英]Convert reduce lambda to list comprehension https://stackoom.com/zh/question/2G0sV 细说Python的lambda函数用法,建议收藏- 知乎- 知乎专栏Webb在Python中有 ...
#74. Python 数组转化为list
WebAug 13, 2019 · list 是Python 中可說是最最常用的資料型態(Data Type), ... Python sort 排序用法與範例ShengYu Talk WebNov 14, 2019 · Python 提供兩種內建排序 ...
#75. Python for 迴圈寫法
使用方法. 範例. 執行結果. range( ...另個for迴圈的應用是將串列和for迴圈結合,使用迭代的方式生成串列(list comprehension),讓程式的可讀性更高也 ...
#76. List lambda排序
Webpython 2.6 vr中的list comprehension和lambda表達式。 python 2.7 [英]list ... Python List 的sort 與sorted 排序用法教學與範例- Office 指南Web介紹在Python 中 ...
#77. For item in list是什么意思
PYTHON WebMar 24, 2014 · The reason the list comprehension syntax is like ... 指令用法,列表循环- 简书WebApr 15, 2021 · v-for指令需要以item in list形式的 ...
#78. Python - List Comprehension - W3Schools
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. Example: Based on a list of fruits, ...
#79. 2023 Python 開發app - yusufkesn.online
用kivy開發第一個python app. 安裝完kivy就可以開發app程式了,這裡演示下hello-world程式,關於kivy更復雜的用法不是本文重點,後面再成文介紹。
#80. 咖啡渣再製2023 - nargiledumani.online
Early on 用法行銷策略概念板橋大遠百美食街推薦原味砒霜. ... How to Use List Comprehension in Python · Windows 10 vs Windows 11 All the Major ...
#81. 包裝員英文- 2023
市場薪資必備能力紙箱英文是carton(聽發音),名詞用法,這個單字廣泛的指各種形狀的紙箱,包含用來 ... How to Use List Comprehension in Python ...
#82. 堅尼地小學- 2023
Rip 用法. ... Yet Anime and Games Still Allowed Suggestive Depictions · How to Use List Comprehension in Python · How to Back Up and Restore Your PS4 s Data ...
#83. 2023 專攻new toeic 文法解題秘技- baymyken.online
... 文法重點,由基礎文法觀念開始,由淺入深分析各種衍生規則、慣用句型及特殊用法,並將複雜 ... which assess your oral and written comprehension, respectively.
#84. 英文五年級- 2023
Part II Activity & Verb 活動【人教版小学英语五年级上册电子课本】教师网提供最新的关于人民教育出版社出版的关于电子课本教材,希望能帮助到大家。 See full list on ...
#85. Mark Kemoel ⃤ 2023 - delilalla.online
蝸牛素顏霜用法. 指南針的發明. 九命怪貓. ... Python selenium ie 教學. ... Volvo s Subscription Service Already Has A Waiting List CarBuzz ...
#86. 大漠孜然風味醬- 2023
... 大漠孜然風味醬2023-04-18 續車牌文件Av 排行Python pandas exercises ... 雄心線上看生物医学工程香港復康力量In on at 用法地方Sound hover 格 ...
#87. 2023 台東麵包店- hamgalsi.online
廢文語錄抖音Sabon 身體磨砂膏用法平花盆Millefeuille nights 中川翔子. ... Hocus Pocus sequel 2022 movie cast list Meet the 3 actors who bring ...
#88. 2023 Thirty three miniatures - yildizadam.online
Python map apply. ... Reading comprehension news. ... Artery Gear fusion tier list and reroll guide Ripene · These Fabulous New York Times ...
#89. 俄文鍵盤對照- 2023 - relate.pw
分號用法英文駕訓班上路神奇情緒療癒法dvd 隱形牙套吃東西.Costco 蜜蜂工坊豆豆龍音樂美善品南瓜饅頭Garmin 跑步動態感測器.福州到台北機票台中美餐廳電腦螢幕監視器祿 ...
#90. Kybella 台灣- 2023 - shoulder.pw
Shop low prices on groceries to build your shopping list or order ... on Feb 28 2022 by our editors Dr Attend 用法车标領導風格北模範圍107.
#91. 2023 Hku dorms - sokopasa.online
Chinese trademark subclasses list. 1881 酒店結業. Peninsula tower. ... Reading comprehension grade 2 worksheets pdf. ... Pcloud 用法.
#92. 2023 紅磡獸醫
Possessive pronouns reading comprehension. Plume hong kong. 407c 冷媒. ... Derma medream 凍齡針用法. ... Foods that contain lectins list. 1028 千百度. 涤纶.
#93. Python中的List Comprehensions(列表推导式/列表解析式)
Python 中的List Comprehensions(列表推导式/列表解析式).md 一、引入初学Python的同学经常会遇到一些看起来很简洁,但又不是很好理解的语句, ...
#94. 一行指令學Python:用Pandas掌握商務大數據分析
最後,迴圈配合串列因為太常使用,在 Python 出現串列表達式(List Comprehension), ... 4-1 for 迴圈基本用法假設 list1 = [1,2,3,4,5],我們希望將裡面的資料都乘 10, ...
#95. 增壓的Python|讓程式碼進化到全新境界(電子書)
3.15 雖然知道這種用法也不錯,但它不是 lambda 的常用用法。 ... 綜合表達式 Python 在 2.0 版本引入的最重要的功能之一是 list 綜合表達式(list comprehension)。
#96. 精通Python 3程式設計 第二版 (電子書) - 第 113 頁 - Google 圖書結果
清單清單(list)是一個由零或多個物件參照所構成的序列。清單所支援的切片與跨越語法 ... 建立清單的另一種做法是,使用 list comprehension—本小節稍後會探討這個主題。
#97. 黑帽Python|給駭客與滲透測試者的Python開發指南 第二版(電子書)
results = list() for i in range(0, len(src), length): ➂ word ... 建立字串的串列推導式(List Comprehension)採用的是布林短路求值技術,這種用法看起來有點花哨。
python list comprehension用法 在 Re: [問題] list的iterator應用問題- 看板Python - 批踢踢實業坊 的推薦與評價
原po的程式碼可以說是generator版本的list comprehension,
什麼是list comprehension?
其實把原敘述的左右小括號改成中括號就是了,如下:
>>> test = [(x,y) for x in range(3) for y in range(x)]
會產生出類似的結果:
>>> test
[(1, 0), (2, 0), (2, 1)]
test會是一個list。這樣子的敘述其實等價於:
>>> test = []
>>> for x in range(3):
for y in range(x):
test.append((x,y))
(從上述程式碼原po大概就可以理解為什麼會有那樣的output了吧!)
list comprehension提供了一個更快速的方式建立起一個有規律的list,
x和y也會被清理掉而不會像下面那種方式依然存在。
但原po的程式碼則叫做generator expression:
>>> test = ((x,y) for x in range(3) for y in range(x))
得到的test會是一個generator,是一種iterator,
可以透過next()取得下一個值直到沒有東西,
>>> next(test)
(1, 0)
>>> next(test)
(2, 0)
>>> next(test)
(2, 1)
>>> next(test)
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
next(test)
StopIteration
也可以像原po一樣透過for迴圈取值出來,
所以會得到類似list comprehension的結果。
那樣的敘述等價於:
>>> def test():
for x in range(3):
for y in range(x):
yield(x,y)
>>> test = test()
generator expression省去了建立generator function,實作iterator class的麻煩。
※ 引述《Neverfor (yorker)》之銘言:
: test=( (x, y) for x in range ( 3 ) for y in range (x) )
: for x,y in test:
: print(x,y)
: output:
: 1 0
: 2 0
: 2 1
: 不好意思 看了很久想不出來程式碼第一行跟結果的關係QQ
: 這種用法是什麼意思呢?
: 想問
: 1. (x,y)是否代表 輸出的iterator 1個elemeny 是 (x,y)
: 2.迴圈看不太懂意思
: 前面的for x 是否是後面y range的x
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.168.202.245
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1467425395.A.84B.html
※ 編輯: max80713 (118.168.202.245), 07/02/2016 10:14:59
※ 編輯: max80713 (118.168.202.245), 07/02/2016 10:21:00
... <看更多>