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 ...
随机推荐
- wraps的补充
# import time# def index(name):# '''index函数'''# time.sleep(1)# print('Welcome %s to China'%name)# re ...
- 提高wifi速度的设置办法
系列的提高wifi速度的设置办法 在DNS一栏有你们家的地址,在你们家的地址前输入“114.114.114.114”并以“,”结尾(注意:要用英文输入法哦.) 设置完后点击左上角的“无线局域网”回到初 ...
- HDU 6073 Matching In Multiplication —— 2017 Multi-University Training 4
Matching In Multiplication Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K ( ...
- Python基础教程(014)--缩进错误
前言 学会编写格式 内容 print(“HelloWorld”) print(“HelloWorld”) ----缩进错误 print(“HelloWorld”) 错误信息: IndentationE ...
- 用C#编写ActiveX控件
http://www.cnblogs.com/homer/archive/2005/01/04/86473.html http://www.cnblogs.com/homer/archive/2005 ...
- python 简单抓取网页并写入excel实例
# -*- coding: UTF-8 -*- import requests from bs4 import BeautifulSoup import xlwt import time #获取第一页 ...
- 修改element中v-loading的自定义图片
/*elementui loading css 覆盖 开始*/ .el-loading-spinner .circular{ width: 42px; height: 42px; animation: ...
- PWN入门的入门——工具安装
安装pwntool: 命令行运行: pip install pwntools python import pwn pwn.asm("xor eax,eax") 出现'1\xc0' ...
- Autoresize UIView to fit subviews
@interface UIView (resizeToFit) -(void)resizeToFitSubviews; -(void)resizeHightToFitSubviews; -(void) ...
- Spring中Bean的作用域和生命周期
作用域的种类 Spring 容器在初始化一个 Bean 的实例时,同时会指定该实例的作用域.Spring3 为 Bean 定义了五种作用域,具体如下. 1)singleton 单例模式,使用 sing ...