python学习笔记——列表推导 list comprehension
来源:互联网 发布:tvc广告制作 动画网络 编辑:程序博客网 时间:2024/05/21 05:23
python 的列表推导的方式进行遍历,这样可以利用python 的特性加快遍历效率
>>> numbers = range(10)>>> size = len(numbers)>>> evens = []>>> i = 0>>> while i < size:... if i % 2 == 0:... evens.append(i)... i += 1... >>> evens[0, 2, 4, 6, 8]>>> [i for i in range(10) if i % 2 == 0][0, 2, 4, 6, 8]>>>
列表推导还可以代替嵌套循环
>>> for i in [1,2,3]:... for j in [1,2,3]:... z = i*j... print z... 123246369>>> print [i*j for i in [1,2,3] for j in [1,2,3]][1, 2, 3, 2, 4, 6, 3, 6, 9]>>> print i*j for i in [1,2,3] for j in [1,2,3] File "<stdin>", line 1 print i*j for i in [1,2,3] for j in [1,2,3] ^SyntaxError: invalid syntax
0 0
- python学习笔记——列表推导 list comprehension
- Python学习笔记:列表推导式List Comprehension
- Python中的列表推导式(list comprehension)
- Python列表推导式(List comprehension)
- python:列表推导式,list comprehension
- Python列表推导式(List comprehension)
- 列表推导(List Comprehension)
- python学习笔记--推导式(Comprehension)
- [Python]学习:黑魔法之列表推导式(list comprehension)
- 11-09-15 python 列表推导List Comprehension&
- Python map/reduce vs 列表推导式[list comprehension]
- Python——List Comprehension
- Python学习笔记(三)——List Comprehension 和 Generator Expression
- Python学习笔记 —— List列表
- python列表解释(list comprehension)记录
- Python学习笔记1---推导列表
- Python List Comprehension, Dictionary Comprehension
- Python 列表解析 例子 讲解 学习 list comprehension in python demo
- 并查集入门
- 微信登录的code获取
- 十步完全理解SQL
- hiho一下 第四十周 题目1 : 三分·三分求极值
- 我的ubuntu软件列表
- python学习笔记——列表推导 list comprehension
- 在400×1的列向量中,1至40每个数顺序出现10次MATLAB代码
- hadoop操作命令行
- HDOJ-5199-Gunner 解题报告
- 使用 AVAudioSession 实现后台播放音乐
- 硬件重构与软件定义
- Android界面刷新的方法
- STL总结
- android中WebView的用法总结