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 ...
随机推荐
- js-ifelse-奇技淫巧
我们有A,B,C,D四个不同的类别,在最开始的时候只有三个类别,并且两个类别是做同样的事: function categoryHandle(category) { if(category !== 'A ...
- javaScript的关键字与保留字
JavaScript 关键字: break case catch continue default delete do else finally for function if in instance ...
- Halo(九)
跨域问题 域名A(http://www.a.com)的 Web 应用程序中, 通过标签引入了域名B(http://ww.b.com)站点的某图片资源(http://www.b.com/image.jp ...
- c++使用boost库遍历文件夹
1.只在当前目录下遍历 #include <boost/filesystem.hpp> string targetPath="/home/test/target"; b ...
- 基于oracle 的PL/SQL编程 - 存储过程
接上篇,游标使用的语句,相当于一段匿名的函数,窗口关闭了就不存在了.如果想要窗口关闭了,还能继续执行那段代码,就需要存储过程了: PLSQL是指一个个PLSQL的业务处理过程存储起来进行复用,这些被存 ...
- [CF852H]Bob and stages
题意:给出平面上\(n\)个点,要求选出\(k\)个点,使得这些点形成一个凸包,且凸包内部没有点,求最大面积.无解输出\(0\). 题解:枚举凸包最左的点\(p\),删除所有在\(p\)左边的点,然后 ...
- BUUCTF | SQL COURSE 1
一开始还以为是在登录框进行注入,于是fuzzing了一下发现一个注入点都没有 1 and 1 1 and 0 1' and '1 1' and '0 1" and "1 1&quo ...
- DZY Loves Math
DZY Loves Math 对于正整数 $n$,定义 $f(n)$ 为 $n$ 所含质因子的最大幂指数. 例如 $f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007)=1, ...
- intellijidea查看git窗口
version control null
- JumpServer堡垒机安装笔记
厂商文档--一步一步安装CentOS(https://jumpserver.readthedocs.io/zh/master/setup_by_centos.html) 厂商文档--简单优化(http ...