python Iteration(迭代)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Iteration(迭代)相关的知识,希望对你有一定的参考价值。
概念:可用于循环就是迭代
#!/usr/bin/python
dic = {'a':1, 'b':2, 'c':3, 'd':4};
for key in dic:
print "key:", key;
for value in dic.itervalues():
print "value:", value;
for key in dic.iterkeys():
print "key:", key;
for key in dic.keys():
print "keys:", key;
for key,value in dic.iteritems():
print "iteritems:", key, value;
#check 检查dic是否为可迭代类型
from collections import Iterable #申明Iterable
print isinstance(dic, Iterable);
#enumerate
for x, y in enumerate(['a', 'b', 'c']):
print x, y
for x,y in [(1, 2), (3, 4), (5, 6)]:
print x,y
for x in [(1, 2), (3, 4), (5, 6)]:
print x
以上是关于python Iteration(迭代)的主要内容,如果未能解决你的问题,请参考以下文章
翻译:《实用的Python编程》06_01_Iteration_protocol
翻译:《实用的Python编程》06_02_Customizing_iteration