最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Python内置函iter

来源:动视网 责编:小采 时间:2020-11-27 14:28:03
文档

Python内置函iter

Python内置函iter:英文文档:iter(object[, sentinel])Return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second arg
推荐度:
导读Python内置函iter:英文文档:iter(object[, sentinel])Return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second arg


英文文档:

iter(object[, sentinel])

Return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, object must be a collection object which supports the iteration protocol (the __iter__() method), or it must support the sequence protocol (the __getitem__() method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised. If the second argument, sentinel, is given, then object must be a callable object. The iterator created in this case will call object with no arguments for each call to its__next__() method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.

One useful application of the second form of iter() is to read lines of a file until a certain line is reached. The following example reads a file until the readline() method returns an empty string:

with open('mydata.txt') as fp: for line in iter(fp.readline, ''):
process_line(line)

说明:

  1. 函数功能返回一个可迭代对象。

  2. 当第二个参数不提供时,第一个参数必须是一个支持可迭代协议(即实现了__iter__()方法)的集合(字典、集合、不可变集合),或者支持序列协议(即实现了__getitem__()方法,方法接收一个从0开始的整数参数)的序列(元组、列表、字符串),否则将报错。

>>> a = iter({'A':1,'B':2}) #字典集合
>>> a

>>> next(a)
'A'
>>> next(a)
'B'
>>> next(a)
Traceback (most recent call last):
 File "", line 1, in 
 next(a)
StopIteration
 
>>> a = iter('abcd') #字符串序列
>>> a

>>> next(a)
'a'
>>> next(a)
'b'
>>> next(a)
'c'
>>> next(a)
'd'
>>> next(a)
Traceback (most recent call last):
 File "", line 1, in 
 next(a)
StopIteration

3. 当第二个参数sentinel提供时,第一个参数必须是一个可被调用对象。创建的迭代对象,在调用__next__方法的时候会调用这个可被调用对象,当返回值和sentinel值相等时,将抛出StopIteration异常, 终止迭代。

# 定义类
>>> class IterTest: 
 def __init__(self):
 self.start = 0
 self.end = 10
 def get_next_value(self):
 current = self.start
 if current < self.end:
 self.start += 1
 else:
 raise StopIteration
 return current

>>> iterTest = IterTest() #实例化类
>>> a = iter(iterTest.get_next_value,4) # iterTest.get_next_value为可调用对象,sentinel值为4
>>> a

>>> next(a)
>>> next(a)
>>> next(a)
>>> next(a)
>>> next(a) #迭代到4终止
Traceback (most recent call last):
 File "", line 1, in 
 next(a)
StopIteration

文档

Python内置函iter

Python内置函iter:英文文档:iter(object[, sentinel])Return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second arg
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top