当前位置:网站首页>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
边栏推荐
- Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
- Global and Chinese market of RF shielding room 2022-2028: Research Report on technology, participants, trends, market size and share
- ByteDance ten years of experience, old bird, took more than half a year to sort out the software test interview questions
- How to solve the poor sound quality of Vos?
- Cc36 different subsequences
- Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
- [pointer] octal to decimal
- 王爽汇编语言详细学习笔记二:寄存器
- Statistics 8th Edition Jia Junping Chapter 1 after class exercises and answers summary
- C language learning summary (I) (under update)
猜你喜欢
Report on the double computer experiment of scoring system based on 485 bus
软件测试方法有哪些?带你看点不一样的东西
How to rename multiple folders and add unified new content to folder names
Get started with Matplotlib drawing
几款开源自动化测试框架优缺点对比你知道吗?
How to learn automated testing in 2022? This article tells you
"If life is just like the first sight" -- risc-v
ucore lab5用户进程管理 实验报告
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
Fundamentals of digital circuits (I) number system and code system
随机推荐
Fundamentals of digital circuits (III) encoder and decoder
软件测试工作太忙没时间学习怎么办?
Don't you even look at such a detailed and comprehensive written software test question?
Get started with Matplotlib drawing
Cadence physical library lef file syntax learning [continuous update]
Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
MySQL development - advanced query - take a good look at how it suits you
With 27K successful entry ByteDance, this "software testing interview notes" has benefited me for life
If the position is absolute, touchablehighlight cannot be clicked - touchablehighlight not clickable if position absolute
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
软件测试面试要问的性能测试术语你知道吗?
C language do while loop classic Level 2 questions
王爽汇编语言学习详细笔记一:基础知识
How to solve the poor sound quality of Vos?
Login the system in the background, connect the database with JDBC, and do small case exercises
The minimum number of operations to convert strings in leetcode simple problem
安全测试入门介绍
[pointer] solve the last person left
几款开源自动化测试框架优缺点对比你知道吗?