list=[1,2,3,4]
it = iter(list) # 创建迭代器对象
for x in it:
print (x, end=" ")
import sys         # 引入 sys 模块

list=[1,2,3,4]
it = iter(list) # 创建迭代器对象 while True:
try:
print (next(it))
except StopIteration:
sys.exit()
class MyNumbers:
def __iter__(self):
self.a = 1
return self def __next__(self):
x = self.a
self.a += 1
return x myclass = MyNumbers()
myiter = iter(myclass) print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
class MyNumbers:
def __iter__(self):
self.a = 1
return self def __next__(self):
if self.a <= 20:
x = self.a
self.a += 1
return x
else:
raise StopIteration myclass = MyNumbers()
myiter = iter(myclass) for x in myiter:
print(x)
import sys

def fibonacci(n): # 生成器函数 - 斐波那契
a, b, counter = 0, 1, 0
while True:
if (counter > n):
return
yield a
a, b = b, a + b
counter += 1
f = fibonacci(10) # f 是一个迭代器,由生成器返回生成 while True:
try:
print (next(f), end=" ")
except StopIteration:
sys.exit()

吴裕雄--天生自然 PYTHON3开发学习:迭代器与生成器的更多相关文章

  1. 吴裕雄--天生自然 PYTHON3开发学习:MySQL - mysql-connector 驱动

    import mysql.connector mydb = mysql.connector.connect( host="localhost", # 数据库主机地址 user=&q ...

  2. 吴裕雄--天生自然 PYTHON3开发学习:字符串

    var1 = 'Hello World!' var2 = "Runoob" #!/usr/bin/python3 var1 = 'Hello World!' var2 = &quo ...

  3. 吴裕雄--天生自然 PYTHON3开发学习:数字(Number)

    print ("abs(-40) : ", abs(-40)) print ("abs(100.10) : ", abs(100.10)) #!/usr/bin ...

  4. 吴裕雄--天生自然 PYTHON3开发学习:运算符

    #!/usr/bin/python3 a = 21 b = 10 c = 0 c = a + b print ("1 - c 的值为:", c) c = a - b print ( ...

  5. 吴裕雄--天生自然 PYTHON3开发学习:基本数据类型

    #!/usr/bin/python3 counter = 100 # 整型变量 miles = 1000.0 # 浮点型变量 name = "runoob" # 字符串 print ...

  6. 吴裕雄--天生自然 PYTHON3开发学习:基础语法

    #!/usr/bin/python3 # 第一个注释 print ("Hello, Python!") # 第二个注释 #!/usr/bin/python3 # 第一个注释 # 第 ...

  7. 吴裕雄--天生自然 PYTHON3开发学习:函数

    def 函数名(参数列表): 函数体 # 计算面积函数 def area(width, height): return width * height def print_welcome(name): ...

  8. 吴裕雄--天生自然 PYTHON3开发学习:元组

    tup1 = ('Google', 'Runoob', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print ("tup1[0]: ", ...

  9. 吴裕雄--天生自然 PYTHON3开发学习:列表

    list1 = ['Google', 'Runoob', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b& ...

随机推荐

  1. JS ~ Promise 对象

    Promise 对象用于表示一个异步操作的最终状态(完成或失败),以及该异步操作的结果值. Promise.all(iterable) 这个方法返回一个新的promise对象,该promise对象在i ...

  2. C++ STD Gems03

    transform.for_each #include <iostream> #include <vector> #include <string> #includ ...

  3. DCGAN

    Deep Convolutional Generative Adversarial Networks we introduced the basic ideas behind how GANs wor ...

  4. C# ASP 面试题 2017

    在博客上看到的,感觉还不错 https://www.cnblogs.com/renyiqiu/p/6435261.html

  5. Centos7.4系统 httpd模式搭建文件服务器

    环境:服务环境:centos7.4 说明:搭建Apache文件服务器,下载路径为/opt/ymyg(下载路径根据实际需要自己定义) 步骤: 1.安装httpd服务   [root@localhost ...

  6. 【SpringBoot】SpringBoot Web开发(八)

    本周介绍SpringBoot项目Web开发的项目内容,及常用的CRUD操作,阅读本章前请阅读[SpringBoot]SpringBoot与Thymeleaf模版(六)的相关内容 Web开发 项目搭建 ...

  7. Uber推出全新交通估算体系能颠覆传统模式吗?

    当下,大众的交通出行正在被全面颠覆.除了传统的出行方式外,共享打车.共享单车.共享滑板车.分时租赁的共享汽车等,正在形成一个全新交通出行矩阵.鉴于交通出行市场的巨大潜力,众多巨头及独角兽企业在绞尽脑汁 ...

  8. continue break

    #!/bin/bashfor i in `seq 10`do    if ((i%3==0))    thenecho !!        continue    fiecho $idone 结果: ...

  9. java-正则表达式判断移动联通电信手机号

    package com.linbilin.phone; import java.util.regex.Matcher; import java.util.regex.Pattern; public c ...

  10. 利用zed相机为rtabmap_ros录制rosbag包及其使用

    1,录制rosbag包 rosbag record /zed_node/rgb/image_rect_color /zed_node/rgb/camera_info /zed_node/depth/d ...