当前位置:网站首页>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
边栏推荐
- Express
- Quaternion -- basic concepts (Reprint)
- Flash implements forced login
- UCORE lab7 synchronous mutual exclusion experiment report
- Fundamentals of digital circuit (V) arithmetic operation circuit
- Function: find the root of the equation by Newton iterative method
- Functions: Finding Roots of equations
- 王爽汇编语言学习详细笔记一:基础知识
- The minimum number of operations to convert strings in leetcode simple problem
- What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
猜你喜欢
Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
STC-B学习板蜂鸣器播放音乐
1. Payment system
Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
The latest query tracks the express logistics and analyzes the method of delivery timeliness
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
几款开源自动化测试框架优缺点对比你知道吗?
Query method of database multi table link
全网最详细的postman接口测试教程,一篇文章满足你
CSAPP家庭作业答案7 8 9章
随机推荐
Fundamentals of digital circuits (III) encoder and decoder
Summary of thread implementation
Software testing interview summary - common interview questions
Global and Chinese market of barrier thin film flexible electronics 2022-2028: Research Report on technology, participants, trends, market size and share
Login the system in the background, connect the database with JDBC, and do small case exercises
To brush the video, it's better to see if you have mastered these interview questions. Slowly accumulating a monthly income of more than 10000 is not a dream.
数字电路基础(二)逻辑代数
The four connection methods of JDBC are directly coded
5 minutes to master machine learning iris logical regression classification
Get started with Matplotlib drawing
软件测试工作太忙没时间学习怎么办?
Express
Leetcode simple question: check whether two strings are almost equal
[pointer] delete all spaces in the string s
CSAPP homework answers chapter 789
数字电路基础(三)编码器和译码器
1. Payment system
安全测试入门介绍
Fundamentals of digital circuits (I) number system and code system
[pointer] counts the number of times one string appears in another string