日期: 2022-09-21 05:35:32 浏览数:4

上往建站提供服务器空间服务商,百度快照排名,网站托管,百度推广运营,致力于设计外包服务与源代码定制开发,360推广,搜狗推广,增加网站的能见度及访问量提升网络营销的效果,主营:网站公司,百度推广公司电话,官网搭建服务,网站服务企业排名,服务器空间,英文域名等业务,专业团队服务,效果好。
泸州爱采购会员/58同城会员账户/百度竞价开户/百家号注册运营/推广公司网站-网站营销全网推广

提到:字符串,列表或元组对象都可用于创建迭代器。
字符串(Strings):
普通的旧字符串也是可迭代的。
for s in "hello":
print s
输出结果为:
h
e
l
l
o
列表(Lists):
这些可能是最明显的迭代。
for x in [None,3,4.5,"foo",lambda : "moo",object,object()]:
print "{0} ({1})".format(x,type(x))
输出结果为:
None (<type 'NoneType'>)
3 (<type 'int'>)
4.5 (<type 'float'>)
foo (<type 'str'>)
<function <lambda> at 0x7feec7fa7578> (<type 'function'>)
<type 'object'> (<type 'type'>)
<object object at 0x7feec7fcc090> (<type 'object'>)
元组(Tuples):
元组在某些基本方面与列表不同,注意到以下示例中的可迭代对象使用圆括号而不是方括号,但输出与上面列表示例的输出相同。
for x in (None,3,4.5,"foo",lambda : "moo",object,object()):
print "{0} ({1})".format(x,type(x))
输出结果为:
None (<type 'NoneType'>)
3 (<type 'int'>)
4.5 (<type 'float'>)
foo (<type 'str'>)
<function <lambda> at 0x7feec7fa7578> (<type 'function'>)
<type 'object'> (<type 'type'>)
<object object at 0x7feec7fcc090> (<type 'object'>)
字典(Dictionaries):
字典是键值对的无序列表。当您使用for循环遍历字典时,您的虚拟变量将使用各种键填充。
d = {
'apples' : 'tasty',
'bananas' : 'the best',
'brussel sprouts' : 'evil',
'cauliflower' : 'pretty good'
}
for sKey in d:
print "{0} are {1}".format(sKey,d[sKey])
输出结果为:
brussel sprouts are evil
apples are tasty
cauliflower are pretty good
bananas are the best
也许不是这个顺序,字典是无序的!!!
really
really
nin***ally9@gmail.com
参考地址
4年前 (2019-01-26)
wuguandong
963***591@qq.com
17
使用自定义迭代器实现斐波那契数列
class Fibonacci:
def __init__(self, count):
self.count = count
def __iter__(self):
self.i = 0
self.a, self.b = 0, 1
return self
def __next__(self):
if self.i < self.count:
self.i += 1
a_old = self.a
self.a, self.b = self.b, self.a + self.b
return a_old
else:
raise StopIteration
for i in Fibonacci(10):
print(i, end=" ")
wuguandong
wuguandong
963***591@qq.com
3年前 (2019-08-21)
闫伟超
yif***chaoran@163.com
73
如教程所说,迭代器和生成器算是 Python 一大特色,其核心是基于迭代器协议来的。
而平时我们经常使用的 for in 循环体,本质
泸州爱采购会员/58同城会员账户/百度竞价开户/百家号注册运营/推广公司网站-网站营销全网推广
上往建站提供搭建网站,域名注册,官网备案服务,网店详情页设计,企业网店,专业网络店铺管理运营全托管公司咨询电话,服务器空间,微信公众号托管,网页美工排版,致力于域名申请,竞价托管,软文推广,全网营销,提供标准级专业技术保障,了却后顾之忧,主营:虚拟主机,网站推广,百度竞价托管,网站建设,上网建站推广服务,网络公司有哪些等业务,专业团队服务,效果好。
服务热线:400-111-6878 手机微信同号:18118153152(各城市商务人员可上门服务)
样我们实际上是先生成了一个 1000 个元素的 list:f,然后我们再去使用这个 f。
现在,我们换一个方法:
因为我们实际使用的是 list 的遍历,也就是 list 的迭代器。那么我们可以让这个函数 fab 每次只返回一个迭代器——一个计算结果,而不是一个完整的 list:
这样,我们每次调用fab函数,比如这样:
或者 next 函数之类的,实际上的运行方式是每次的调用都在 yield 处中断并返回一个结果,然后再次调用的时候再恢复中断继续运行。
ErikaEmma
463***503@qq.com
参考地址
collector
120***9047@qq.com
对yield的测试结果:
def get(): m = 0 n = 2 l = ['s',1,3] k = {1:1,2:2} p = ('2','s','t') while True: m += 1 yield m yield m ,n ,l ,k ,p it = get()print(next(it)) #1print(next(it)) #(1, 2, ['s', 1, 3], {1: 1, 2: 2}, ('2', 's', 't'))print(next(it)) #2print(type(next(it))) #<class 'tuple'>collector
120***9047@qq.com
打个比方的话,yield有点像断点。 加了yield的函数,每次执行到有yield的时候,会返回yield后面的值 并且函数会暂停,直到下次调用或迭代终止;
yield后面可以加多个数值(可以是任意类型),但返回的值是元组类型的。
怀雨
522***18@qq.com
def get(): m = 0 n = 2 l = ['s',1,3] k = {1:1,2:2} p = ('2','s','t') while True: m += 1 yield m yield m ,n ,l ,k ,p it = get()print(next(it)) #1print(next(it)) #(1, 2, ['s', 1, 3], {1: 1, 2: 2}, ('2', 's', 't'))print(next(it)) #2print(type(next(it))) #<class 'tuple'>如果再加一句:
所以返回值的类型,应该是当前调用时,yield 返回值的类型。
怀雨
522***18@qq.com
hid4net
hid***t@qq.com
感谢楼上各位的实例,自己写了一个小例程,便于我这样的新手理解。
def myYield_1(): a, i = 'yield', 0 while True: print('before #%d' % i, end=", ") yield a, i print('after #%d' % i, end=", ") i += 1def myYield_2(): a, i = 'yield_a', 0 b, i = 'yield_b', 0 while True: print('before #%d' % i, end=", ") yield a, i yield b, i print('after #%d' % i, end=", ") i += 1it1 = iter(myYield_1())it2 = iter(myYield_2())for i in range(10): print("next #%d" % i, end=": ") print(next(it1))print(' ')for i in range(10): print("next #%d" % i, end=": ") print(next(it2))输出是这样的:
next #0: before #0, ('yield', 0)next #1: after #0, before #1, ('yield', 1)next #2: after #1, before #2, ('yield', 2)next #3: after #2, before #3, ('yield', 3)next #4: after #3, before #4, ('yield', 4)next #5: after #4, before #5, ('yield', 5)next #6: after #5, before #6, ('yield', 6)next #7: after #6, before #7, ('yield', 7)next #8: after #7, before #8, ('yield', 8)next #9: after #8, before #9, ('yield', 9)next #0: before #0, ('yield_a', 0)next #1: ('yield_b', 0)next #2: after #0, before #1, ('yield_a', 1)next #3: ('yield_b', 1)next #4: after #1, before #2, ('yield_a', 2)next #5: ('yield_b', 2)next #6: after #2, before #3, ('yield_a', 3)next #7: ('yield_b', 3)next #8: after #3, before #4, ('yield_a', 4)next #9: ('yield_b', 4)hid4net
hid***t@qq.com
really
nin***ally9@gmail.com
参考地址
前文提到:字符串,列表或元组对象都可用于创建迭代器。
字符串(Strings):
普通的旧字符串也是可迭代的。
输出结果为: