当前位置:网站首页>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
边栏推荐
- Global and Chinese market of barrier thin film flexible electronics 2022-2028: Research Report on technology, participants, trends, market size and share
- The maximum number of words in the sentence of leetcode simple question
- C language learning summary (I) (under update)
- With 27K successful entry ByteDance, this "software testing interview notes" has benefited me for life
- What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
- [oiclass] maximum formula
- Numpy快速上手指南
- 数字电路基础(三)编码器和译码器
- 刷视频的功夫,不如看看这些面试题你掌握了没有,慢慢积累月入过万不是梦。
- 全网最详细的postman接口测试教程,一篇文章满足你
猜你喜欢
王爽汇编语言学习详细笔记一:基础知识
Leetcode simple question: check whether two strings are almost equal
Want to learn how to get started and learn software testing? I'll give you a good chat today
In Oracle, start with connect by prior recursive query is used to query multi-level subordinate employees.
The number of reversing twice in leetcode simple question
Fundamentals of digital circuits (II) logic algebra
Wang Shuang's detailed notes on assembly language learning I: basic knowledge
王爽汇编语言详细学习笔记二:寄存器
[200 opencv routines] 98 Statistical sorting filter
Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
随机推荐
[pointer] counts the number of times one string appears in another string
Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)
软件测试方法有哪些?带你看点不一样的东西
The minimum number of operations to convert strings in leetcode simple problem
Global and Chinese market of pinhole glossmeter 2022-2028: Research Report on technology, participants, trends, market size and share
How to use Moment. JS to check whether the current time is between 2 times
Cc36 different subsequences
{1,2,3,2,5} duplicate checking problem
Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
Four methods of exchanging the values of a and B
Global and Chinese market of portable and handheld TVs 2022-2028: Research Report on technology, participants, trends, market size and share
China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
安全测试入门介绍
接口测试面试题及参考答案,轻松拿捏面试官
如何成为一个好的软件测试员?绝大多数人都不知道的秘密
JDBC 的四种连接方式 直接上代码
C language do while loop classic Level 2 questions
[HCIA continuous update] advanced features of routing
Using flask_ Whooshalchemyplus Jieba realizes global search of flask