日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中类怎么理解_Python中的列表理解

發布時間:2025/3/11 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中类怎么理解_Python中的列表理解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

python中類怎么理解

In order to create a list, a most obvious and remembered solution is to use a for-loop.

為了創建列表,最明顯和記住的解決方案是使用for循環。

Example:

例:

Python 3.6.8 (default, Apr 25 2019, 21:02:35) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux Type "help", "copyright", "credits" or "license" for more information.>>> flights = {'09:35':'Long Beach', '10:00':'los-angeles', '11:00':'san jose'} >>> flight_time_list = [] >>> for key in flights.keys(): ... flight_time_list.append(key) ... >>> print(flight_time_list) ['09:35', '10:00', '11:00'] >>>

Python's built-in comprehension feature that lets us reduce the number of lines in list creation to a single line.

Python的內置理解功能使我們可以將列表創建中的行數減少為一行。

Example:

例:

-bash-4.2$ python3 Python 3.6.8 (default, Apr 25 2019, 21:02:35) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux Type "help", "copyright", "credits" or "license" for more information.>>> flights = {'09:35':'Long Beach', '10:00':'los-angeles', '11:00':'san jose'} >>> flight_time_list = [ key for key in flights.keys()] >>> print(flight_time_list) ['09:35', '10:00', '11:00'] >>>

列表理解的語法 (Syntax for list comprehension)

The list comprehension starts with a [ and ], to ensure that the result is a list.

列表理解以[和]開頭,以確保結果為列表。

[expression for item in list]

The comprehension can also utilize the if condition.

理解也可以利用if條件 。

Example: Grouping the common items to a list

示例:將常見項目分組到列表中

Python 3.6.8 (default, Apr 25 2019, 21:02:35) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux Type "help", "copyright", "credits" or "license" for more information.>>> grade_k = ['maddy', 'sriansh', 'owen', 'molly'] >>> grade_first = ['molly', 'owen', 'ricky', 'sid'] >>> common_names = [a for a in grade_k for b in grade_first if a==b] >>> print(common_names) ['owen', 'molly'] >>>

使用理解的優勢 (Advantages of using Comprehension)

  • Comprehensions requires less code. Python interpreter is optimized to run comprehensions as quickly as possible.

    理解需要更少的代碼。 Python解釋器經過優化,可以盡快運行理解。

  • Comprehensions execute faster than for loop.

    理解的執行速度比for循環快。

  • Comprehensions can be used in places where for loop cannot be used. All the comprehensions appear to the right of the assignment operator, which is something for loop cannot do.

    可以在無法使用for循環的地方使用理解 。 所有的理解都出現在賦值運算符的右邊,這是for循環無法做到的。

  • 翻譯自: https://www.includehelp.com/python/list-comprehension.aspx

    python中類怎么理解

    總結

    以上是生活随笔為你收集整理的python中类怎么理解_Python中的列表理解的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。