python 迭代器和iter()函数详解及实例
(编辑:jimmy 日期: 2025/10/25 浏览:3 次 )
python中迭代器和iter()函数
迭代器为类序列对象提供了一个类序列的接口。python的迭代无缝地支持序列对象,而且它还允许程序员迭代非序列类型,包括用户定义的对象。迭代器用起来很灵巧,你可以迭代不是序列但表现处序列行为的对象,例如字典的键、一个文件的行,等等。迭代器的作用如下:
"htmlcode">
#iter and generator
#the first try
#=================================
i = iter('abcd')
print i.next()
print i.next()
print i.next()
s = {'one':1,'two':2,'three':3}
print s
m = iter(s)
print m.next()
print m.next()
print m.next()
D:\Scirpt\Python\Python高级编程>python ch2_2.py
a
b
c
{'three': 3, 'two': 2, 'one': 1}
three
two
one
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
下一篇:Unicode和Python的中文处理