当前位置:网站首页>Iterators and generators
Iterators and generators
2022-07-06 15:09:00 【Naive code writing】
Iteratable object
list 、 Tuples 、 aggregate 、 Dictionaries 、 Objects such as strings are called iteratable objects
iterator :
x=[1,2,3]
i=iter(x)
print(next(i))
print(next(i))
print(next(i))#next Only one number is iterated out each time , Iterate from front to back , There are several numbers that can be iterated several times , More than will prompt that there is no iteratible object
generator :
Use yield :
Complex generator :
def gen(n):
for i in range(n):
yield i*i
x=gen(5)
for i in x:
print(i)
Output results :
0
1
4
9
16
Simplification generator :
a=(i*i for i in range(5))
for i in a:
print(i)
The output is the same
What does the generator do : Save storage space and time , The first step is to directly generate the function , There is no need to calculate ,
import sys
import time
t1=time.time()
mylist = [i for i in range(10000000)]
t2=time.time()
print(" Elapsed time :",t2-t1)
print(" Occupancy space :",sys.getsizeof(mylist))
t3=time.time()
mygen = (i for i in range(10000000))
t4=time.time()
print(" Elapsed time :",t4-t3)
print(" Occupancy space :",sys.getsizeof(mygen))
Output results :
Elapsed time : 0.5699965953826904
Occupancy space : 89095160
Elapsed time : 0.0
Occupancy space : 104
practice :
Construct a generator to calculate the absolute value .
b=[1,2,3,-4,-5,-3]
a=(abs(x) for x in b )# At this time a It's a list of all positive numbers
for i in a:
print(i)
Output results :
1
2
3
4
5
3
边栏推荐
- 基于485总线的评分系统双机实验报告
- What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
- CSAPP Shell Lab 实验报告
- Keil5-MDK的格式化代码工具及添加快捷方式
- ucore lab5用户进程管理 实验报告
- Fundamentals of digital circuits (III) encoder and decoder
- ucore lab6 调度器 实验报告
- Public key box
- Opencv recognition of face in image
- Global and Chinese market of goat milk powder 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
自动化测试你必须要弄懂的问题,精品总结

王爽汇编语言学习详细笔记一:基础知识

Wang Shuang's detailed learning notes of assembly language II: registers

Rearrange spaces between words in leetcode simple questions
![Cadence physical library lef file syntax learning [continuous update]](/img/0b/75a4ac2649508857468d9b37703a27.jpg)
Cadence physical library lef file syntax learning [continuous update]

Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code

Wang Shuang's detailed notes on assembly language learning I: basic knowledge

后台登录系统,JDBC连接数据库,做小案例练习

接口测试面试题及参考答案,轻松拿捏面试官
随机推荐
Wang Shuang's detailed learning notes of assembly language II: registers
Numpy Quick Start Guide
Function: find 1-1/2+1/3-1/4+1/5-1/6+1/7-... +1/n
数字电路基础(三)编码器和译码器
Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
The number of reversing twice in leetcode simple question
[issue 18] share a Netease go experience
How to use Moment. JS to check whether the current time is between 2 times
数字电路基础(五)算术运算电路
Investment should be calm
指針:最大值、最小值和平均值
About the garbled code problem of superstar script
Global and Chinese market of goat milk powder 2022-2028: Research Report on technology, participants, trends, market size and share
UCORE lab8 file system experiment report
Leetcode simple question: check whether two strings are almost equal
Function: calculates the number of uppercase letters in a string
Summary of thread implementation
The maximum number of words in the sentence of leetcode simple question
[Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases
ucore lab8 文件系统 实验报告