List comprehensions provide a concise way to create new lists, where each item is the result of an operation applied to each member of an existing list, dictionary or other iterable. Learn how to create your own list comprehensions in this lesson.

sales = [3.14, 7.99, 10.99, 0.99, 1.24]

sales = [sale * 1.07 for sale in sales]

With condiion:

zoo_animals = ['giraffe', 'monkey', 'elephant', 'lion', 'bear', 'pig', 'horse', 'aardvark']
my_animals = ['monkey', 'bear', 'pig'] other_animals = [animal for animal in zoo_animals if animal not in my_animals]

Recommended way to do:

other_animals = [
animal
for animal in zoo_animals
if animal not in other_animals
]

Break down to multi lines make things looks more clear

[Python] Understand List Comprehensions in Python的更多相关文章

  1. Python中的Comprehensions和Generations

    Python中的Comprehensions和Generations语法都是用来迭代的.Comprehensions语法可用于list,set,dictionary上,而Generations语法分为 ...

  2. [Python's] Python's list comprehensions a

    # Python's list comprehensions are awesome. vals = [expression for value in collection if condition] ...

  3. Comprehensive learning path – Data Science in Python深入学习路径-使用python数据中学习

    http://blog.csdn.net/pipisorry/article/details/44245575 关于怎么学习python,并将python用于数据科学.数据分析.机器学习中的一篇非常好 ...

  4. [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本

    黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...

  5. 【python之路1】python安装与环境变量配置

    直接搜索 Python,进入官网,找到下载,根据个人电脑操作系统下载相应的软件.小编的是windows os .下载python-2.7.9.msi 安装包  双击安装程序,进入安装步骤.在安装过程中 ...

  6. 使用python+xpath 获取https://pypi.python.org/pypi/lxml/2.3/的下载链接

    使用python+xpath 获取https://pypi.python.org/pypi/lxml/2.3/的下载链接: 使用requests获取html后,分析html中的标签发现所需要的链接在& ...

  7. 【Python网络编程】利用Python进行TCP、UDP套接字编程

    之前实现了Java版本的TCP和UDP套接字编程的例子,于是决定结合Python的学习做一个Python版本的套接字编程实验. 流程如下: 1.一台客户机从其标准输入(键盘)读入一行字符,并通过其套接 ...

  8. 【转】linux和windows下安装python集成开发环境及其python包

    本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...

  9. 从零开始学Python第0周:Python基本介绍(部分内容来源于网络)

    Python入门介绍 一,Python的基本介绍 (1)概要 Python是一种解释型,面向对象,动态数据类型的高级程序设计语言.常被广泛用于处理系统管理任务和web编程.现如今Python已经成为了 ...

随机推荐

  1. less12 函数

    less .x(1) { x:11 } .x(2) { y:22 } .x(@x:1) when (default()) {z:@x} //default()表示一直为真 body{ backgrou ...

  2. WinRar 设置默认的压缩格式为zip

    By default, WinRar uses the RAR archive format for compressing files. You may prefer using the more ...

  3. 8.queue

    #include <iostream> #include <stack> #include <algorithm> #include <list> #i ...

  4. AngularJs轻松入门(一)创建第一个应用

    AngularJs是Google工程师研发的一款JS框架,官方文档中对它的描述是,它是完全使用JavaScript编写的客户端技术,同其他历史悠久的Web技术(HTML,CSS等)配合使用,使得Web ...

  5. gym 100971 J Robots at Warehouse

    Vitaly works at the warehouse. The warehouse can be represented as a grid of n × m cells, each of wh ...

  6. python实例

    先来一段代码: #这段代码可牛逼了,1.可以根据indent的选项调整模式.2.根据level调整级别. #代码很low,主要看思想..哈哈哈..看看从最初的样子到最好经历了什么.. 开始: #!/u ...

  7. 关于H5中 input消除默认,取消在手机上的点击高亮效果

    input消除默认,代码如下    input{             -webkit-tap-highlight-color: rgba(255, 255, 255, 0);            ...

  8. NetBios, NetBios over TCP/IP, SMB 之间的关系

    首先提到的是NetBios,NetBios是Network Basic Input/Output System的缩写,提供了一种允许局域网内不同电脑能够通信的功能.严格来说,NetBios是一套API ...

  9. 一篇文章助你理解Python2中字符串编码问题

    前几天给大家介绍了unicode编码和utf-8编码的理论知识,没来得及上车的小伙伴们可以戳这篇文章:浅谈unicode编码和utf-8编码的关系.下面在Python2环境中进行代码演示,分别Wind ...

  10. WHU 1552 Seats 枚举

    题意: 有一个年级中7个班的n个学生. 一天,他们毫无顺序的站成一排.请计算最小的交换次数,使得 相同班的同学都站在一起. (只有站在一起的人才能交换) 思路: 如果知道班级的最终排列就能在很短的时间 ...