Python 推導

在本文中,我們將看到如何推導 Python 的數據結構,如列表、字典、集合和生成器。

推導提供了一種用 Python 編寫程序的精確方式。它在不影響代碼可讀性的情況下減少了代碼大小。

所以,在這裡我們將討論以下推導-

  1. 列表推導
  2. 字典推導
  3. 集合推導
  4. 生成器推導

列表推導

我們知道列表的元素被括在方括號中,它可以保存多種數據類型的值。

在下面給出的程序中,我們將從列表中取出偶數。

讓我們看看下面的列表示例。

示例-


list1= [20,25,24,30,35,40,44]
list2=[]
for i in list1:
    if i%2==0:
        list2.append(i)
print("The elements of list2 are :" ,list2)

輸出-

The elements of list2 are : [20, 24, 30, 40, 44]

這裡我們已經指定了 list1 的元素,然後使用 for循環來獲取每個元素,並使用模數運算符檢查它是否能被 2 整除。

在下面給出的程序中,我們使用列表推導進行了同樣的操作。

示例-2-使用列表推導


list1=[20,25,24,30,35,40,44]
list2=[i for i in list1 if i%2==0]
print("The elements obtained using list comprehension are :" ,list2)

輸出-

The elements obtained using list comprehension are : [20, 24, 30, 40, 44]

在這裡,我們可以觀察到,我們在清單 2 中提供了推導,我們在一行中使用了循環和決策。

下一個程序是基於獲取列表 1 中所有元素的立方體。

示例- 3


list1=[2,3,4,5,6,7,8,9,10]
list2=[]
for i in list1:
    list2.append(i**3)
print("The cube of the elements present in list1 is: ",list2)

輸出-

The cube of the elements present in list1 is:  [8, 27, 64, 125, 216, 343, 512, 729, 1000]

我們使用 append()方法,將每個元素的立方體存儲在列表 2 中,然後顯示它。

我們可以用列表推導來做同樣的事情-

示例-4-使用列表推導


list1=[2,3,4,5,6,7,8,9,10]
list2=[i**3 for i in list1 ]
print("The cube of the elements obtained by list comprehension is: ",list2)

輸出-

The cube of the elements obtained by list comprehension is:  [8, 27, 64, 125, 216, 343, 512, 729, 1000]

字典推導

我們都知道字典使用鍵值對,讓我們來看看顯示這些鍵值對的程序。

示例-


fruits=['Apple','Bananas','Custard Apple','Pineapple','Blueberries']
color=['Red','Yellow','Green','Brown','Violet']
result_dict={}
for key,value in zip(fruits,color):
    result_dict[key]=value
print("The resultant dictionary would be: ",result_dict)

輸出-

The resultant dictionary would be:  {'Apple': 'Red', 'Bananas': 'Yellow', 'Custard Apple': 'Green', 'Pineapple': 'Brown', 'Blueberries': 'Violet'}

在下一個程序中,同樣的事情用推導來實現

示例-2-使用字典推導


fruits=['Apple','Bananas','Custard Apple','Pineapple','Blueberries']
color=['Red','Yellow','Green','Brown','Violet']
result_dict={key:value for (key,value) in zip(fruits,color)}
print("The resultant dictionary using comprehension would be: ",result_dict)

輸出-

The resultant dictionary using comprehension would be:  {'Apple': 'Red', 'Bananas': 'Yellow', 'Custard Apple': 'Green', 'Pineapple': 'Brown', 'Blueberries': 'Violet'}

在這裡,我們可以觀察到我們在 result_dict 中提供了推導,其中我們給出了用於顯示來自列表水果和顏色的鍵值對的表達式。

集合推導

Set 用於顯示給定集合中的唯一元素。讓我們用集合獲得列表所有元素的平方。

示例- 1


list1=[2,3,4,5,6,7,8,9,10]
result_set=set()
for i in list1:
    result_set.add(i**2)
print("The square of the numbers present in list1 is: ",result_set)

輸出-

The square of the numbers present in list1 is:  {64, 4, 36, 100, 9, 16, 49, 81, 25}

在下面給出的程序中,我們用推導做了同樣的事情。

示例- 2-使用集合推導


list1=[2,3,4,5,6,7,8,9,10]
result_set={i**2 for i in list1}
print("The square of the numbers obtained through set comprehension: ",result_set)

輸出-

The square of the numbers obtained through set comprehension:  {64, 4, 36, 100, 9, 16, 49, 81, 25}

我們從列表 1 中提取了每個元素,並在 result_set 中提供了計算這些元素平方的表達式。

生成器推導

生成器的功能非常相似。它使用 yield 關鍵字生成一個值。讓我們看看推導在這裡是如何運用的。

示例-


list1=[12,16,17,20,21,24,28,30,31]
result_gen=(i for i in list1 if i%2==0)
for i in result_gen:
    print("The element which is even in list1 is: ",i)

輸出-

The element which is even in list1 is:  12
The element which is even in list1 is:  16
The element which is even in list1 is:  20
The element which is even in list1 is:  24
The element which is even in list1 is:  28
The element which is even in list1 is:  30

在執行程序時,它顯示列表 1 中的偶數元素。


原創文章,作者:QMZFS,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/316109.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
QMZFS的頭像QMZFS
上一篇 2025-01-09 12:14
下一篇 2025-01-09 12:14

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • Python編程二級證書考試相關現已可以上網購買

    計算機二級Python考試是一項重要的國家級認證考試,也是Python編程的入門考試。與其他考試一樣,Python編程二級證書的考生需要進入正式考試,而為了備考,這篇文章將詳細介紹…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • 蝴蝶優化算法Python版

    蝴蝶優化算法是一種基於仿生學的優化算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化算法Python版…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29

發表回復

登錄後才能評論