Python列表生成式(入门9)
转载请标明出处:
http://www.cnblogs.com/why168888/p/6407984.html本文出自:【Edwin博客园】
Python列表生成式
1. 生成列表
L = []
for x in range(1, 11):
L.append(x * x)
print L
print '\n'
print [x * x for x in range(1, 11)]
print '\n'
print [x * (x + 1) for x in range(1, 100, 2)]
2. 复杂表达式
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
tds = ['<tr><td>%s</td><td>%s</td></tr>' % (name, score) for name, score in d.iteritems()]
print '<table>'
print '<tr><th>Name</th><th>Score</th><tr>'
print '\n'.join(tds)
print '</table>'
print '\n'
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
def generate_tr(name, score):
if score < 60:
return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score)
return '<tr><td>%s</td><td>%s</td></tr>' % (name, score)
tds = [generate_tr(name, score) for name, score in d.iteritems()]
print '<table border="1">'
print '<tr><th>Name</th><th>Score</th><tr>'
print '\n'.join(tds)
print '</table>'
3. 条件过滤
print [x * x for x in range(1, 11)]
print '\n'
print [x * x for x in range(1, 11) if x % 2 == 0]
print '\n'
def toUppers(L):
return [x.upper() for x in L if isinstance(x, str)]
print toUppers(['Hello', 'world', 101])
4. 多层表达式
print [m + n for m in 'ABC' for n in '123']
print '\n'
L = []
for m in 'ABC':
for n in '123':
L.append(m + n)
print L
print '\n'
print [100 * n1 + 10 * n2 + n3 for n1 in range(1, 10) for n2 in range(10) for n3 in range(10) if n1==n3]
Python列表生成式(入门9)的更多相关文章
- Python 列表生成式 & 字典生成式
Python 列表生成式 & 字典生成式 通过生成式可以更加简洁地生成列表和字典 列表生成式 对比 直接生成数据后加入列表示例: user_list = list() for i in ran ...
- Python 列表生成式、生成器、迭代器
列表生成式 列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式. 如果要生成[1x1, 2x2, 3x3, ..., 10x10]怎么 ...
- python列表生成式
在Python编程中,list列表应用的比较广泛,而列表生成式是一个非常强大但是使用很简单的功能. 比如要生成1到100的数组列表[1,2,3,4......,97,98,99,100],可以使用li ...
- python 列表生成式,生成器&迭代器
列表生成式: 需求:要对列表 [0,1,2,3,4,5,6,7,8,9]的每个元素加1 用列表生成式一步搞定: li = [i+1 for i in range(10)] # 这种写法就叫列表生成式 ...
- 初学Python——列表生成式、生成器和迭代器
一.列表生成式 假如现在有这样一个需求:快速生成一个列表[1,2,3,4,5,6,7,8,9,10],该如何实现? 在不知道列表生成式的情况下,可能会这样写: a=[1,2,3,4,5,6,7,8,9 ...
- 【370】Python列表生成式(for 写入一行)
参考: python用列表生成式写嵌套循环的方法_python_脚本之家 参考: python之列表生成式 - 旷野足迹 - 博客园 对于读取Twitter数据最终会得到一个超级复杂的json文件, ...
- 【转】Python 列表生成式
原文:https://blog.csdn.net/heartyhu/article/details/50988007 1. 生成列表 要生成list [1, 2, 3, 4, 5, 6, 7, 8, ...
- python 列表生成式、lower()和upper()的使用
参考: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868196389 ...
- python列表生成式、键盘输入及类型转换、字符串翻转、字母大小写、数组广播、循环语句等基础问题
Python知识总结 1.列表生成式 在实际开发过程中,当需要获取一个连续列表时,可直接使用range(3,10),但是如果获取该列表中每个数据的平方时,通常可以通过for循环来解决这个问题,如下 ...
随机推荐
- 【Css】Layout布局(一)
看下图: css框模型(Box Model),也有叫做盒模型的.规定了元素框处理元素内容.内边距.边框 和 外边距 的方式. element元素,也是内容的主体: padding内边距,也右称为填充的 ...
- HTML5定位使用心得
最近项目中接到一个需求,需要使用百度地图API和HTML5定位特性. 对于百度地图API,没啥好说的,刨它的API就是了. 今天想说说JS定位特性. 浏览器支持 Internet Explorer 9 ...
- Windows加密API的功能分类
本地数据加密保护本地数据加密保护机制提供了简单的DAPI调用接口,密钥管理等等一概由系统来处理.DAPI的数据加密保护机制在用户登录会话范围或者本地计算范围,使用操作系统设计的方式加密保护数据和解密还 ...
- Codeforces 671 A——Recycling Bottles——————【思维题】
Recycling Bottles time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Dell解决黑苹果网卡(BCM94352ZAE/DW1560)怎么都打不开WiFi
Dell解决黑苹果网卡(BCM94352ZAE/DW1560)怎么都打不开WiFi 2017年10月20日17:41:00 by SemiconductorKING 本来觉得驱动这个网卡不是个问题,以 ...
- ASP.NET MVC4 新手入门教程之七 ---7.向电影模式和表中添加新字段
在这一节中,您将使用实体框架代码第一次迁移,迁移到模型类的一些变化,所以该更改应用于数据库. 默认情况下,当您使用实体框架代码优先将自动创建一个数据库,像你那样早些时候在本教程中,代码第一次添加一个表 ...
- jQuery实现单击和鼠标感应事件。
1.实现单击事件动态交替http://www.cnblogs.com/ahthw/p/4232837.html讲到了toggleClass(),对于单击事件而言,jQuery同样提供了动态交替的tog ...
- Vue-Router的使用(一)
1.首先,安装vue-router npm install vue-router --save-dev 2.创建一个route.js文件 // 1. 定义路由组件 // 可以自己写的,或者导入的,大部 ...
- vc++返回模块路径
#include "stdafx.h"#include <Windows.h>#include <string.h>const int MAXPATHLEN ...
- LeetCode Palidrome Number
class Solution { public: bool isPalindrome(int x) { ) return false; ; int t = x; ; ) { pow *= ; cnt+ ...