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)

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

    理解需要更少的代码。 Python解释器经过优化,可以尽快运行理解。

  2. Comprehensions execute faster than for loop.

    理解的执行速度比for循环快。

  3. 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中的列表理解相关推荐

  1. python 生成器表达式_Python中的列表理解与生成器表达式

    python 生成器表达式 The list is a collection of different types of elements and there are many ways of cre ...

  2. python 定义list长度_python中list列表的高级函数 python如何统计列表的长度

    在python的函数中,如何将列表list的一部分作为函比如定义个函数,想实现的功能就是将列表a的后半部分(['c','d'])传入后面paraTestList(a[2:])中,括号里面的a[2:]命 ...

  3. python去除excel空行_python中删除列表中的空元素以及如何读取excel中的数据

    这个暂时也没有找到更好的办法,用的是别人博客中的一种办法http://www.biofacebook.com/?p=186 while " in a: a.remove(") 其中 ...

  4. python中类的用法_Python中的类和方法使用举例

    成员变量 对象的创建 创建对象的过程称之为实例化,当一个对象被创建后,包含三个方面的特性对象聚丙属性和方法, 句柄用于区分不同的对象, 对象的属性和方法,与类中的成员变量和成员函数对应, obj = ...

  5. python中类的嵌套_python中的嵌套类 | 学步园

    在.NET和JAVA语言中看到过嵌套类的实现,作为外部类一个局部工具还是很有用的,今天在python也看到了很不错支持一下.动态语言中很好的嵌套类的实现,应该说嵌套类解决设计问题同时简化了程序,值得学 ...

  6. python中的self怎么理解_python中的self理解

    前言 先介绍下类和实例 面向对象最重要的概念就是类(class)和实例(instance),类时抽象的模板,比如学生这个抽象的事物,可以用一个student类来表示.而实例时根据类创建出来的一个个具体 ...

  7. python中类的嵌套_python 中的嵌套类

    嵌套类的简单介绍 在看 idarling 源码中,经常出现如下代码: import sys import ida_funcs import ida_kernwin from PyQt5.QtCore ...

  8. python中类的构成_Python中类型关系和继承关系实例详解

    本文详细介绍了Python中类型关系和继承关系.分享给大家供大家参考.具体分析如下: 如果一个对象A持有另一个对象B的ID,那么检索到A之后就可以检索到B,我们就说存在一个A到B的导航.这种导航关系使 ...

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

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

最新文章

  1. cocos2d-js中listView的jumpToBottom()方法无效的解决方法
  2. 4、Mysql 主从复制报错[ERROR] [MY-013117] 踩坑
  3. gcc-5.4.0 static dwarf2 compile
  4. RESTful 架构详解
  5. 20145227《信息安全系统设计基础》第一周学习总结
  6. 本地tomcat 配置环境变量
  7. 学机械也想转嵌入式?
  8. 台积电5nm生产线污染原因查明:不影响A15芯片量产
  9. CCNA学习指南十三章
  10. C#取得指定路径下所有目录及文件名称(可递归)
  11. php趣味编程 - php 余弦曲线
  12. 【渝粤教育】国家开放大学2018年秋季 0032-21T农业经济学 参考试题
  13. QQ群统一规范与守则
  14. Excel如何快速根据身份证号码计算周岁?
  15. 2020年python哪个版本好用_不要再纠结Python哪个版本好,2020年用Python3就对了
  16. xml--通过DOM解析XML
  17. SAR变化检测的性能指标(kappa系数)——简化版
  18. 入门级移动App服务器的软硬件需求
  19. h5公众号分享朋友、朋友圈
  20. 大类资产配置(三)市场择时能力模型T-M

热门文章

  1. UVAoj 11324 - The Largest Clique(tarjan + dp)
  2. core文件如何分析
  3. android判断点击次数_Android应用统计-使用时长及次数统计(一)
  4. java判断对称素数_SM2非对称算法的原理及实现 Java SM2的代码案例 | 一生孤注掷温柔 | 小奋斗...
  5. 0 0/2 * * * ? linux文本含义,Linux基础2.0
  6. linux下调用python脚本,Linux下QT调用Python脚本的解决方案,Qt,python,一种,解决办法
  7. 系统分析师和系统架构设计师难度比较_系统架构设计师,马上开课了!
  8. php外部对象如何使用方法,php面向对象全攻略 (三)特殊的引用“$this”的使用...
  9. Mac使用Homebrew安装Kafka
  10. Redis(三):Redis基础知识与常用命令