python 生成器表達式_Python中的列表理解與生成器表達式

python 生成器表達式

The list is a collection of different types of elements and there are many ways of creating a list in Python.

該列表是不同類型元素的集合,并且有許多方法可以在Python中創建列表。

清單理解 (List Comprehension)

List comprehension is one of the best ways of creating the list in one line of Python code. It is used to save a lot of time in creating the list.

列表理解是在一行Python代碼中創建列表的最佳方法之一。 它用于節省創建列表的大量時間。

Let's take an example for a better understanding of the list comprehension that calculates the square of numbers up to 10. First, we try to do it by using the for loop and after this, we will do it by list comprehension in Python.

讓我們以一個示例為例,以更好地理解列表推導,該推導可以計算最多10個數字的平方。首先,我們嘗試使用for循環進行此操作,然后,在Python中通過列表推導進行此操作。

By using the for loop:

通過使用for循環:

List_of_square=[]
for j in range(1,11):
s=j**2
List_of_square.append(s)
print('List of square:',List_of_square)

Output

輸出量

List of square: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Now, we do it by List comprehension,

現在,我們通過列表理解來做到這一點,

List_of_square=[j**2 for j in range(1,11)]
print('List of square:',List_of_square)

Output

輸出量

List of square: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

As we have seen that the multiple lines of code of for loop gets condensed into one line of code in the list comprehension and also saves the execution time.

如我們所見,for循環的多行代碼在列表理解中被壓縮為一行代碼,還節省了執行時間。

生成器表達式 (Generator Expression)

A generator expression is slightly similar to list comprehension but to get the output of generators expression we have to iterate over it. It is one of the best ways to use less memory for solving the same problem that takes more memory in the list compression. Here, a round bracket is used instead of taking output in the form of the list. Let’s look at an example for a better understanding of generator expression that will calculate the square of even numbers up to 20.

生成器表達式與列表理解有些相似,但是要獲得生成器表達式的輸出,我們必須對其進行迭代。 這是使用較少的內存來解決相同的問題(在列表壓縮中占用更多內存)的最佳方法之一。 在此,使用圓括號代替列表形式的輸出。 讓我們看一個示例,以更好地理解生成器表達式,該表達式將計算最多20個偶數的平方。

Program:

程序:

generators_expression=(j**2 for j in range(1,21) if j%2==0)
print('square of even number:')
for j in generators_expression:
print(j, end=' ')

Output

輸出量

square of even number:
4 16 36 64 100 144 196 256 324 400

翻譯自: https://www.includehelp.com/python/list-comprehension-vs-generators-expression.aspx

python 生成器表達式

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379532.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379532.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379532.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Javaweb---監聽器

1.什么是監聽器 監聽器就是監聽某個對象的狀態變化的組件。 事件源:被監聽的對象 ----- 三個域對象 request session servletContext 監聽器:監聽事件源對象 事件源對象的狀態的變化都會觸發監聽器 ---- 62 注冊監聽器:將監聽器與事件源進行…

Linux中的Ramdisk和Initrd

Ramdisk簡介先簡單介紹一下ramdisk,Ramdisk是虛擬于RAM中的盤(Disk)。對于用戶來說,能把RAM disk和通常的硬盤分區(如/dev/hda1)同等對待來使用,例如:redice # mkfs.ext2 /dev/ram0mke2fs 1.38 (30-Jun-200…

slab下kmalloc內核函數實現

文章目錄kmalloc的整體實現獲取高速緩存高速緩存獲取index總結https://blog.csdn.net/qq_41683305/article/details/124554490,在這篇文章中,我們介紹了伙伴算法、slab機制和常見的內存管理函數,接下來,我們看看kmalloc內核函數的…

PHP array_merge_recursive()函數與示例

PHP array_merge_recursive()函數 (PHP array_merge_recursive() function) array_merge_recursive() function is used to merge two or more arrays, it returns a new array with merged elements. The only difference between array_merge() and array_merge_recursive() …

標題:三羊獻瑞

標題:觀察下面的加法算式: 其中,相同的漢字代表相同的數字,不同的漢字代表不同的數字。 請你填寫“三羊獻瑞”所代表的4位數字(答案唯一),不要填寫任何多余內容。 思路分析: 首先…

hdu 1069

地址&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1069 題意&#xff1a;給定若干個木塊長寬高&#xff0c;長寬高可以自己調整&#xff0c;求堆積起來最高的高度。 mark&#xff1a;枚舉所有木塊長寬高可能情況&#xff0c;簡單dp。 代碼&#xff1a; #include <…

簡明 Python 編程規范

簡明 Python 編程規范編碼 所有的 Python 腳本文件都應在文件頭標上 # -*- coding:utf-8 -*- 。設置編輯器&#xff0c;默認保存為 utf-8 格式。注釋 業界普遍認同 Python 的注釋分為兩種的概念&#xff0c;一種是由 # 開頭的“真正的”注釋&#xff0c;另一種是 docstri…

進程虛擬地址管理

文章目錄1 地址分布實際使用中的內存區域2 進程的虛擬地址描述用戶空間mmap線程之間共享內存地址的實現機制1 地址分布 現在采用虛擬內存的操作系統通常都使用平坦地址空間&#xff0c;平坦地址空間是指地址空間范圍是一個獨立的連續空間&#xff08;比如&#xff0c;地址從0擴…

java兩個文件夾比較路徑_比較Java中兩個文件的路徑

java兩個文件夾比較路徑Given the paths of the two files and we have two compare the paths of the files in Java. 給定兩個文件的路徑&#xff0c;我們有兩個比較Java中文件的路徑。 Comparing paths of two files 比較兩個文件的路徑 To compare the paths of two file…

標題:加法變乘法

標題&#xff1a;我們都知道&#xff1a;123 … 49 1225 現在要求你把其中兩個不相鄰的加號變成乘號&#xff0c;使得結果為2015 比如&#xff1a; 123…10*1112…27*2829…49 2015 就是符合要求的答案。 請你尋找另外一個可能的答案&#xff0c;并把位置靠前的那個乘號左…

C# winform對話框用法大全收藏

對話框中我們常用了以下幾種&#xff1a; 1、文件對話框(FileDialog) 它又常用到兩個&#xff1a; 打開文件對話框(OpenFileDialog) 保存文件對話(SaveFileDialog) 2、字體對話框(FontDialog) 3、顏色對話框(&#xff23;olorDialog) 4、打印預瀏對話框(PrintPreviewDialog) 5、…

【翻譯】eXpressAppFramework QuickStart 業務模型設計(四)—— 實現自定義業務類...

這一講&#xff0c;你將學到如何從頭開始實現業務類。為此&#xff0c;將要實現Department和Position業務類。這些類將被應用到之前實現的Contact類中。你將學到引用對象自動生成用戶界面的基本要素。 在此之前&#xff0c;我建議你去閱讀一下 【翻譯】eXpressAppFramework Qui…

內存重映射

文章目錄1 kmap2 映射內核內存到用戶空間使用remap_pfn_range使用io_remap_pfn_rangemmap文件操作建立VMA和實際物理地址的映射mmap 之前分配 一次性映射mmap 之前分配 Page FaultPage Fault 中分配 映射內核內存有時需要重新映射&#xff0c;無論是從內核到用戶空間還是從內…

math.sqrt 有問題_JavaScript中帶有示例的Math.sqrt()方法

math.sqrt 有問題JavaScript | Math.sqrt()方法 (JavaScript | Math.sqrt() Method) The Math.sqrt() method is inbuilt in JavaScript to find the square root of a number. In this tutorial, we will learn about the sqrt() method with examples. JavaScript中內置了Mat…

標題:移動距離

標題&#xff1a;移動距離 X星球居民小區的樓房全是一樣的&#xff0c;并且按矩陣樣式排列。其樓房的編號為1,2,3… 當排滿一行時&#xff0c;從下一行相鄰的樓往反方向排號。 比如&#xff1a;當小區排號寬度為6時&#xff0c;開始情形如下&#xff1a; 1 2 3 4 5 6 12 11 1…

ISAPI Rewrite 實現簡單url重寫、二級域名重寫

實現步驟&#xff1a; 第一步&#xff1a;下載ISAPI_Rewrite.rar&#xff0c;將Rewrite文件夾和httpd.ini直接放在項目根目錄下面。 第二步&#xff1a;IIS配置&#xff0c;篩選Rewrite文件夾里面的Rewrite.dll文件&#xff0c;如圖&#xff1a; 第三步&#xff1a;在httpd.ini…

用戶登錄

用戶登錄 代碼namespace 用戶登錄 {public partial class Form1 : Form{public Form1(){InitializeComponent();}bool b1, b2, b3, b4, b5, b6;private void button1_Click(object sender, EventArgs e){try{if (b1 && b2 && b3 && b4 && b5 &…

進程上下文和中斷上下文

文章目錄進程的preempt_count變量thread_infopreempt_counthardirq相關softirq相關上下文原文鏈接&#xff1a; https://zhuanlan.zhihu.com/p/88883239進程的preempt_count變量 thread_info 在內核中&#xff0c;上下文的設置和判斷接口可以參考 include/linux/preempt.h 文…

標題:湊算式

標題&#xff1a;湊算式 這個算式中AI代表19的數字&#xff0c;不同的字母代表不同的數字。 比如&#xff1a; 68/3952/714 就是一種解法&#xff0c; 53/1972/486 是另一種解法。 這個算式一共有多少種解法&#xff1f; 注意&#xff1a;你提交應該是個整數&#xff0c;不要…

匯編中imul_JavaScript中帶有示例的Math.imul()方法

匯編中imulJavaScript | Math.imul()方法 (JavaScript | Math.imul() Method) Math.imul() is a function in math library of JavaScript that is used to the 32-bit multiplication of the two values passed to it. It uses C-like semantics to find the multiplication. …