List Comprehension ()(二)
2024-10-07 15:22:47
在list comprehension中加入if条件判断:
>>> lines = [line.rstrip() for line in open('script2.py') if line[0] == 'p']
>>> lines
['print(sys.path)', 'print(x ** 32)']
这段代码相当于:
>>> res = []
>>> for line in open('script2.py'):
... if line[0] == 'p':
... res.append(line.rstrip())
...
>>> res
['print(sys.path)', 'print(x ** 32)']
在list comprehension中加入嵌套循环:
>>> [x+y for x in 'abc' for y in 'lmn']
['al', 'am', 'an', 'bl', 'bm', 'bn', 'cl', 'cm', 'cn']
这段代码相当于:
>>> res = []
>>> for x in 'abc':
... for y in 'lmn':
... res.append(x+y)
...
>>> res
['al', 'am', 'an', 'bl', 'bm', 'bn', 'cl', 'cm', 'cn']
最新文章
- 在 C# 中定义一个真正只读的 List
- Sql Server 覆盖索引
- 基础才是重中之重~C#中标准的IDispose模式
- Daily Scrum – 1/4
- 自定义FragmentTabHost--实现View重复加载问题
- html5--等待加载效果
- Spring切面处理
- python的sorted相关
- VS2008 ctrl+shift+F热键冲突
- BZOJ 2733: [HNOI2012]永无乡(treap + 启发式合并 + 并查集)
- java实现线性表
- 数组中的reduce 函数理解
- 关于ES6
- python selenium ——— 动态id、class定位
- C#客户端和服务端数据的同步传输 (转载)
- redis的spring的xml配置
- QXcbConnection: Could not connect to display
- BZOJ.4753.[JSOI2016]最佳团体(01分数规划 树形背包DP)
- 第 3 章 HTML5 网页中的文本和图像
- Java并发编程-synchronized指南
热门文章
- boost location-dependent times
- Web项目改名的带来的404not found问题
- linux网络接口,struct ifreq struct ifconf结构
- Where should I put <;script>; tags in HTML markup?
- ";如何用70行Java代码实现深度神经网络算法"; 的delphi版本
- Linux 通配符和特殊符号
- [题解]RGB Substring (hard version)-前缀和(codeforces 1196D2)
- java中四种访问修饰符区别及详解全过程
- 正则findall的使用
- ontouchstart ondragstart=";return false"; oncopy=";return false;"; oncut=";return false onselectstart=";return false"; onpaste=";return false";