python基本 迭代
2015-12-05 16:56
573 查看
From:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868196435255fcca20a1630446ea2dd434a7176e152000
dict默认迭代key
如果要同时迭代key和value,可以用
tuple 迭代
如何判断一个对象是可迭代对象呢?方法是通过collections模块的Iterable类型判断:
dict默认迭代key
>>> d = {'a': 1, 'b': 2, 'c': 3} >>> for key in d: ... print key ... a c b
如果要同时迭代key和value,可以用
for k, v in d.iteritems()
>>> for k, v in d.iteritems(): print k, v a 1 c 3 b 2
tuple 迭代
>>> for x, y,z in [(1,1,11), (2, 4,6), (3, 9,5)]: print x,y,z 1 1 11 2 4 6 3 9 5
如何判断一个对象是可迭代对象呢?方法是通过collections模块的Iterable类型判断:
>>> from collections import Iterable >>> isinstance('abc', Iterable) # str是否可迭代 True >>> isinstance([1,2,3], Iterable) # list是否可迭代 True >>> isinstance(123, Iterable) # 整数是否可迭代 False
相关文章推荐
- python基础教程共60课-第6课bool
- Python爬虫入门五之URLError异常处理
- python int to binary and count the number of 1
- Python爬虫入门四之Urllib库的高级用法
- python编码检测模块chardet
- Python爬虫入门三之Urllib库的基本使用
- Python爬虫入门二之爬虫基础了解
- Python爬虫入门一之综述
- [转]逻辑斯蒂回归 via python
- Python 爬虫学习系列教程----目录
- python--10行代码搞定词频统计
- python基础教程共60课-第5课变量
- python基础教程共60课-第4课输入
- Python——参数(2)
- python--删除队列重复元素
- win64位系统安装numpy
- python基本 if和function
- python实现关闭第三方窗口
- python 多线程/多进程
- 机器学习python实战——K近邻算法