首页 > 编程语言 > 详细

python Iteration(迭代)

时间:2018-07-16 14:54:20      阅读:168      评论:0      收藏:0      [点我收藏+]
概念:可用于循环就是迭代



#!/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(迭代)

原文:http://blog.51cto.com/13502993/2144285

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!