在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']

最新文章

  1. 在 C# 中定义一个真正只读的 List
  2. Sql Server 覆盖索引
  3. 基础才是重中之重~C#中标准的IDispose模式
  4. Daily Scrum – 1/4
  5. 自定义FragmentTabHost--实现View重复加载问题
  6. html5--等待加载效果
  7. Spring切面处理
  8. python的sorted相关
  9. VS2008 ctrl+shift+F热键冲突
  10. BZOJ 2733: [HNOI2012]永无乡(treap + 启发式合并 + 并查集)
  11. java实现线性表
  12. 数组中的reduce 函数理解
  13. 关于ES6
  14. python selenium ——— 动态id、class定位
  15. C#客户端和服务端数据的同步传输 (转载)
  16. redis的spring的xml配置
  17. QXcbConnection: Could not connect to display
  18. BZOJ.4753.[JSOI2016]最佳团体(01分数规划 树形背包DP)
  19. 第 3 章 HTML5 网页中的文本和图像
  20. Java并发编程-synchronized指南

热门文章

  1. boost location-dependent times
  2. Web项目改名的带来的404not found问题
  3. linux网络接口,struct ifreq struct ifconf结构
  4. Where should I put <script> tags in HTML markup?
  5. "如何用70行Java代码实现深度神经网络算法" 的delphi版本
  6. Linux 通配符和特殊符号
  7. [题解]RGB Substring (hard version)-前缀和(codeforces 1196D2)
  8. java中四种访问修饰符区别及详解全过程
  9. 正则findall的使用
  10. ontouchstart ondragstart="return false" oncopy="return false;" oncut="return false onselectstart="return false" onpaste="return false"