当前位置:网站首页>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
边栏推荐
- [200 opencv routines] 98 Statistical sorting filter
- ucore lab5用户进程管理 实验报告
- 基于485总线的评分系统双机实验报告
- Query method of database multi table link
- STC-B学习板蜂鸣器播放音乐2.0
- 指針:最大值、最小值和平均值
- Leetcode simple question: check whether the numbers in the sentence are increasing
- Summary of thread implementation
- ucore lab1 系统软件启动过程 实验报告
- Emqtt distribution cluster and node bridge construction
猜你喜欢

"If life is just like the first sight" -- risc-v

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

Four methods of exchanging the values of a and B

The minimum sum of the last four digits of the split digit of leetcode simple problem

Sleep quality today 81 points

Stc-b learning board buzzer plays music 2.0

What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
![Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)](/img/85/d6b196f22b60ad5003f73eb8d8a908.png)
Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)

Query method of database multi table link

Wang Shuang's detailed notes on assembly language learning I: basic knowledge
随机推荐
Detailed introduction to dynamic programming (with examples)
Cc36 different subsequences
[pointer] use the insertion sorting method to arrange n numbers from small to large
“Hello IC World”
What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
CSAPP Shell Lab 实验报告
数字电路基础(五)算术运算电路
How to solve the poor sound quality of Vos?
Global and Chinese markets for GaN on diamond semiconductor substrates 2022-2028: Research Report on technology, participants, trends, market size and share
With 27K successful entry ByteDance, this "software testing interview notes" has benefited me for life
Pointer -- eliminate all numbers in the string
指針:最大值、最小值和平均值
150 common interview questions for software testing in large factories. Serious thinking is very valuable for your interview
Functions: Finding Roots of equations
UCORE lab1 system software startup process experimental report
Fundamentals of digital circuits (I) number system and code system
Global and Chinese market of DVD recorders 2022-2028: Research Report on technology, participants, trends, market size and share
Don't you even look at such a detailed and comprehensive written software test question?
ucore lab8 文件系统 实验报告
Common Oracle commands