适才发现之前在pydev中使用的语法检查器是python3.2,而解释器是python2.7……罪过罪过,从此篇之后,不出意外都是使用python3.x版本解释器
本文主要举例介绍python的built-in函数iter,以及类中的两个函数__iter__,__next__。其中作为iterator的对象是类,而不是容器(因为容器或generator作为iterator对象很简单,这里不多说了)
iter函数有两种用法,一种是传一个参数,一种是传两个参数。返回的结果都是返回一个iterator对象。
先说传两个参数的,比如有
i1 = iter(itr, 'c')
这个意思是说,返回itr的iterator,而且在之后的迭代之中,迭代出来'c'就立马停止。对这个itr有什么要求呢?这个itr在这里必须是callable的,即要实现__call__函数
再说传一个参数,有
i2 = iter(itr)
这里itr必须实现__iter__函数,这个函数的返回值必须返回一个iterator对象
看例子:
class Itr(object):
def __init__(self):
self.result = ['a', 'b', 'c', 'd']
self.i = iter(self.result)
def __call__(self):
res = next(self.i)
print("__call__ called, which would return ", res)
return res
def __iter__(self):
print("__iter__ called")
return iter(self.result)
itr = Itr()
# i1必须是callable的,否则无法返回callable-iterator
i1 = iter(itr, 'c')
print("i1 = ", i1)
# i2只需要类实现__iter__函数即可返回
i2 = iter(itr)
print("i2 = ", i2)
for i in i1:
print(i)
for i in i2:
print(i)
输出结果是:
i1 = <callable_iterator object at 0x1349710>
__iter__ called
i2 = <list_iterator object at 0x133a090>
__call__ called, which would return a
a
__call__ called, which would return b
b
__call__ called, which would return c
a
b
c
d
可以看到传入两个参数的i1的类型是一个callable_iterator,它每次在调用的时候,都会调用__cal__函数,并且最后到c就停止了。
而i2就简单的多,itr把自己类中一个容器的迭代器返回就可以了。
有朋友可能不满意,对上面的例子只是为了介绍iter()函数传两个参数的功能而写,如果真正想写一个iterator的类,需要使用__next__函数。这个函数每次返回一个值就可以实现迭代了。
class Next(object):
def __init__(self, data = 1):
self.data = data
def __iter__(self):
return self
def __next__(self):
print("__next__ called")
if self.data > 5:
raise StopIteration
else:
self.data += 1
return self.data
for i in Next(3):
print(i)
输出结果是:
__next__ called
4
__next__ called
5
__next__ called
6
__next__ called
很简单把。唯一需要注意下的就是__next__中必须控制iterator的结束条件,不然就死循环了。
分享到:
相关推荐
python库,解压后可用。 资源全名:mdf_iter-0.0.2-cp38-cp38-win32.whl
list、tuple等都是可迭代对象,我们可以通过iter()函数获取这些可迭代对象的迭代器。然后我们可以对获取到的迭代器不断使⽤next()函数来获取下⼀条数据。iter()函数实际上就是调⽤了可迭代对象的 __iter__ ⽅法。 >...
opencv_face_detector_uint8.pb和 res10_300x300_ssd_iter_140000_fp16.caffemodel 下载
VGG_VOC0712_SSD_300x300_iter_120000.caffemodel,caffe-SSD下已经训练好的VGG模型文件,希望对大家有所帮助。
前端开源库-array-iter-next-crnt数组iter下一个crnt,逐个获取数组项并记住当前项。
使用时需要计算md5sum值放在文件名之前,然后拷贝到opencv-3.4.0/.cache/dnn_face_detector下 md5sum值: afbb6037fd180e8d2acb3b58ca737b9e
ssd的caffee模型
faceparsing_79999_iter.pth,人脸分割faceparsing的预训练模型,PyTorch版本。
给定 max z=cTx ...函数接口:[x_opt,fx_opt,iter] = DSimplex_eye(A,b,c) 其中x_opt为最求解,fx_opt为最优函数值,iter为迭代次数。 例:A=[-1 -2 -1 1 0; -2 1 -3 0 1]; b=[-3 -4]'; c=[-2 -3 -4 0 0]';
res10_300x300_ssd_iter_140000.caffemodel与deploy.prototxt
pose_iter_584000.caffemodel) coco(pose_deploy_linevec.prototxt; pose_iter_440000.caffemodel) mpi(pose_deploy_linevec.prototxt; pose_deploy_linevec_faster_4_stages.prototxt; pose_iter_160000.caffe...
I0701 12:37:55.655375 32319 caffe.cpp:155] Finetuning from models/VGGNet/VOC0712Plus/SSD_300x300/VGG_VOC0712Plus_SSD_300x300_iter_240000.caffemodel I0701 12:37:56.035286 32319 net.cpp:761] Ignoring ...
OpenPose body25 PyTorch model file pose_iter_584000.caffemodel.pt
运行download_weights.py时无法自动下载DNN(opencv)人脸检测模型。所以单独到网站上历经千辛万苦下载得到。
OpenCV实现的SSD人脸检测器,基于深度学习框架Caffe训练的模型。
pose_iter_440000.caffemodel pose_iter_116000.caffemodel pose_iter_102000.caffemo 用官方的OpenPose包里的cmd命令下载第三方库文件和第三方包下载速度极其的慢,我下载好了,一起打包!
人脸识别caffe模型文件
openpose需要的caffemodel:pose_iter_440000、pose_iter_102000、pose_iter_116000、pose_iter_584000、pose_iter_160000
Note: SSD300* and SSD512* are the latest models. Current code should reproduce these results.
openpose1.5.1(2019年)中训练好的姿态数据。openpose中的批处理文件下载速度极慢,且容易中断。下载后将此文件解压放在openpose根目录下的\models\pose\coco文件夹中。