List Comprehension ()(一)
>>> L = [1,2,3,4,5]
>>> L = [x+10 for x in L]
>>> L
[11, 12, 13, 14, 15]
list comprehension使代码变得简洁。下面这种常用的方法则显得过时:
>>> L = [1,2,3,4,5]
>>> for i in range(len(L)):
... L[i]+=10
...
>>> L
[11, 12, 13, 14, 15]
事实上在使用list comprehension时,代码内部相当于执行下面操作:
>>> L = [1,2,3,4,5]
>>> res = []
>>> for x in L:
... res.append(x+10)
...
>>> res
[11, 12, 13, 14, 15]
在可以的使用的地方尽可能使用list comprehension,是一种地道的Python写法。另外,相比for循环,list comprehension在编译器内部的执行效率接近C,速度更快。
List Comprehension ()(一)的更多相关文章
- nim也玩一行流,nim版的list comprehension
nim 是一门风格类似python的静态编译型语言,官方网站:http://nim-lang.org 如果你想折腾nim的编辑环境,可以用sublime text3 +插件nimlime,notepa ...
- Python从题目中学习:List comprehension
九九乘法表作业其实有更简单的做法,就是用列表推导式. ------------------------------------------------------------------------- ...
- python list comprehension twos for loop 嵌套for循环
list comprehension 后面可以有多个for loops,每个for后面可以有if [(x, y, x * y)for x in(0,1,2,3)for y in(0,1,2,3)if ...
- 论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification
论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification 目前,阅读理解通常会给出 ...
- Haskell语言学习笔记(91)Comprehension Extensions
Comprehension Extensions 关于解析式的相关语言扩展. List and Comprehension Extensions 24 Days of GHC Extensions: ...
- Python List Comprehension
(一)使用List Comprehension的好处 在了解Python的List Comprehension之前,我们习惯使用for循环创建列表,比如下面的例子: numbers = range(1 ...
- 链表推导式 【list comprehension】
x for x in x 链表推导式 [list comprehension]链表推导式提供了一个创建链表的简单途径,无需使用 map(), filter() 以及 lambda.返回链表的定义通常要 ...
- Attention-over-Attention Neural Networks for Reading Comprehension论文总结
Attention-over-Attention Neural Networks for Reading Comprehension 论文地址:https://arxiv.org/pdf/1607.0 ...
- list comprehension & generator expression
List comprehensions(列表推导式) are better when you want to iterate over something multiple times. Howeve ...
- Python 列表解析list comprehension和生成表达式generator expression
如果想通过操作和处理一个序列(或其他的可迭代对象)来创建一个新的列表时可以使用列表解析(List comprehensions)和生成表达式(generator expression) (1)list ...
随机推荐
- pipelines和重定向命令
pipelines: command1 | command2 例如,ls -l /usr/bin | less,将输出结果作为 less 命令的输入结果,在standard output 中显示出来. ...
- spring-boot整合mongodb多数据源的案例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...
- ARC093 F Dark Horse——容斥
题目:https://atcoder.jp/contests/arc093/tasks/arc093_d #include<cstdio> #include<cstring> ...
- 洛谷 P3806 (点分治)
题目:https://www.luogu.org/problem/P3806 题意:一棵树,下面有q个询问,问是否有距离为k的点对 思路:牵扯到树上路径的题都是一般都是点分治,我们可以算出所有的路径长 ...
- Distribution money
Distribution money Accepts: 713 Submissions: 1881 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- Instagram几个queryhash
page_query_hash 42323d64886122307be10013ad2dcc44comment_query_hash 33ba35852cb50da46f5b5e889df7d159f ...
- Cent OS (一)Cents OS的基本安装
1.实验环境: VMware Workstation Pro 14 Pro Cent OS 7 系列. 2. 镜像地址传送门: 阿里云开源镜像站:http://mirrors.aliyun.com ...
- HDU-4475 Downward paths(找规律)
Downward paths Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- getAttribute 与getParmeter 区别
1.getAttribute是取得jsp中 用setAttribute設定的attribute 2.parameter得到的是string:attribute得到的是object 3.request. ...
- xshell的安装及连接linux的使用方法
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/lx_Frolf/article/deta ...