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 生成器表达式

python 生成器表达式_Python中的列表理解与生成器表达式相关推荐

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

    python中类怎么理解 In order to create a list, a most obvious and remembered solution is to use a for-loop. ...

  2. python callable对象_Python中callable的理解?

    >Python has a more general concept of callable object, that is every object that can be called, w ...

  3. python list 去重_Python中对列表list去重

    自己写直接的方法 保留原顺序. old_list = [2, 3, 4, 5, 1, 2, 3] new_list = [] for i in old_list:if i not in new_lis ...

  4. python 类中定义列表_Python-从类定义中的列表理解访问类变量

    小编典典 类范围和列表,集合或字典的理解以及生成器表达式不混合. 为什么:或者,官方用词 在Python 3中,为列表理解赋予了它们自己的适当范围(本地名称空间),以防止其局部变量渗入周围的范围内(即 ...

  5. python中result的用法_关于Python中的列表理解及用法

    在Python中,列表理解通常用于编写单行语句,这些语句通过可迭代对象进行迭代以创建新的列表或字典.本文首先介绍for循环如何在Python中工作,然后解释如何在Python中使用列表理解. Pyth ...

  6. 如何用python生成软件_python中的生成器函数是如何工作的?

    1. python中的普通函数是怎么运行的? 当一个python函数在执行时,它会在相应的python栈帧上运行,栈帧表示程序运行时函数调用栈中的某一帧.想要获得某个函数相关的栈帧,则必须在调用这个函 ...

  7. python函数能返回列表吗_Python函数返回列表还是作为生成器工作?

    我正在尝试创建一个返回对象或作为生成器的函数. 这是一个坏主意,因为作为一种最佳实践,您希望函数可靠地返回相同类型的值,但为了科学的利益-- 我正在使用Python 2,因此range返回一个列表,x ...

  8. python中的列表理解_掌握『Python列表理解』需要知道的9件事,你知道了吗?

    越来越多的人开始学习Python,它已经成为最流行的编程语言之一,这几乎发生在所有领域.比如网络开发.科学计算,当然还有人工智能. 无论想用python干什么,都绕不开学习Python的数据结构.变量 ...

  9. python检查列表是否为空_Python 中判断列表是否为空的方法

    在判断列表是否为空时,你更喜欢哪种方式?决定因素是什么? 在 Python 中有很多检查列表是否是空的方式,在讨论解决方案前,先说一下不同方法涉及到的不同因素. 我们可以把判断表达式可以分为两个阵营: ...

最新文章

  1. 两个php的build文件,PHP编译安装中遇到的两个错误和解决方法
  2. 【Python】特征选择方法
  3. java怎么设有滚动的标签,html标签overflow属性和javascript实现div标签滚动
  4. [转载]Tomcat 6.0 安装配置
  5. mac 建立软链接_MAC要倒闭了吗?正装口红80块,生姜高光105,让爱老师存点钱吧!...
  6. 超详细的Java面试题总结(三)之Java集合篇常见问题
  7. 分离圆环图显示百分比_excel这个百分比图,你不一定会制作
  8. word 的脚注横线和文字怎么调整为左对齐?
  9. 查看job运行时间,以便不影响生产数据库正常运行
  10. Vibe算法简介、优缺点、代码
  11. kx驱动中的DSP设置
  12. 下行法求最小割集案例_故障树分析方法(FTA)
  13. v-infinite-scroll懒惰加载
  14. c51单片机蜂鸣器汇编语言,51单片机控制蜂鸣器播放5首歌曲汇编程序
  15. mmdetection3d的安装和使用
  16. linux I2C设备驱动
  17. 泛型的意义和作用是啥?
  18. 2021山东省莱阳市高考成绩查询,2021年莱阳市高考状元名单资料,今年莱阳市高考状元多少分...
  19. 2016年蓝桥杯A组 第九题 密码脱落
  20. 什么是模块化?为什么要模块化

热门文章

  1. tomcat mysql如何优化_Tomcat+Mysql高并发配置优化讲解
  2. 学生机房管理服务器系统设计,广东工业大学数据库课程设计机房管理系统设计...
  3. [one day one question] express 不缓存如何实现
  4. js css模仿打字效果
  5. vue组件-使用插槽分发内容(slot)
  6. (网页)中的简单的遮罩层
  7. html5中如何去掉input type date默认
  8. 2019.7.16考试总结
  9. springboot-RequestMappingHandlerMapping
  10. 网页性能优化(初窥)