python高级特性:Iteration(迭代)

概念:可用于循环就是迭代

网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、小程序制作、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了天坛街道免费建站欢迎大家使用!

#!/usr/bin/python

dic = {'a':1, 'b':2, 'c':3, 'd':4};
for key in dic:
    print "key:", key;

python 高级特性:Iteration(迭代)

for value in dic.itervalues():
    print "value:", value;

python 高级特性:Iteration(迭代)

for key in dic.iterkeys():
    print "key:", key;

python 高级特性:Iteration(迭代)

for key in dic.keys():
    print "keys:", key;

python 高级特性:Iteration(迭代)

for key,value in dic.iteritems():
    print "iteritems:", key, value;

python 高级特性:Iteration(迭代)

#check 检查dic是否为可迭代类型
from collections import Iterable  #申明Iterable
print isinstance(dic, Iterable);

python 高级特性:Iteration(迭代)

#enumerate
for x, y in enumerate(['a', 'b', 'c']):
    print x, y

python 高级特性:Iteration(迭代)

for x,y in [(1, 2), (3, 4), (5, 6)]:
    print x,y

python 高级特性:Iteration(迭代)

for x in [(1, 2), (3, 4), (5, 6)]:
    print x

python 高级特性:Iteration(迭代)


文章题目:python高级特性:Iteration(迭代)
网页链接:http://scyanting.com/article/psjhos.html