什么是迭代? 在python语言中,迭代的意思就是通过for循环遍历一个有序/无序的集合. 迭代是通过 for x in 集合 来完成的. 前面有提到python中有哪些集合,现在我们来梳理一下: (1)有序集合:list ,tuple,str 和unicode. (2)无序集合:set (3)无序集合并且具有key-value对应关系:dict 直接举个代码例子: for循环迭代数列 1-10并打印出5的倍数. >>> L = range(1,11) >>> for
初学Python(八)——迭代 初学Python,主要整理一些学习到的知识点,这次是迭代. # -*- coding:utf-8 -*- from collections import Iterable ''''' 迭代 ''' L = ['af','st','at','psst','beta'] D = {1:'af',2:'st',3:'at',4:'psst',5:'beta'} S = 'helloworld' #数组 for item in L: print item #字典 for
Python中的可迭代对象有:列表.元组.字典.字符串:常结合for循环使用: 判断一个对象是不是可迭代对象: from collections import Iterable isinstance(list(range(100)), Iterable) isinstance('Say YOLO Again.') 列表: L = list(range(100)) for i in L: print(i) 元组: T = tuple(range(100)) for i in T: print(