在本文中,我們將看到如何推導 Python 的數據結構,如列表、字典、集合和生成器。
推導提供了一種用 Python 編寫程序的精確方式。它在不影響代碼可讀性的情況下減少了代碼大小。
所以,在這裡我們將討論以下推導-
- 列表推導
- 字典推導
- 集合推導
- 生成器推導
列表推導
我們知道列表的元素被括在方括號中,它可以保存多種數據類型的值。
在下面給出的程序中,我們將從列表中取出偶數。
讓我們看看下面的列表示例。
示例-
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