当前位置:网站首页>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 4 Summary and after class exercise answers
- [pointer] solve the last person left
- Emqtt distribution cluster and node bridge construction
- Using flask_ Whooshalchemyplus Jieba realizes global search of flask
- [pointer] the array is stored in reverse order and output
- Express
- Face and eye recognition based on OpenCV's own model
- MySQL development - advanced query - take a good look at how it suits you
- Pointer -- output all characters in the string in reverse order
- 指針:最大值、最小值和平均值
猜你喜欢
Opencv recognition of face in image
基于485总线的评分系统双机实验报告
Keil5 MDK's formatting code tool and adding shortcuts
Login the system in the background, connect the database with JDBC, and do small case exercises
C language do while loop classic Level 2 questions
The minimum number of operations to convert strings in leetcode simple problem
Summary of thread implementation
Dlib detects blink times based on video stream
DVWA exercise 05 file upload file upload
ucore lab8 文件系统 实验报告
随机推荐
Cadence physical library lef file syntax learning [continuous update]
数字电路基础(二)逻辑代数
ucore lab1 系统软件启动过程 实验报告
Using flask_ Whooshalchemyplus Jieba realizes global search of flask
The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
Investment should be calm
Function: string storage in reverse order
Fundamentals of digital circuits (II) logic algebra
CSAPP Shell Lab 实验报告
China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
UCORE lab2 physical memory management experiment report
pytest
Query method of database multi table link
Wang Shuang's detailed learning notes of assembly language II: registers
Global and Chinese market of pinhole glossmeter 2022-2028: Research Report on technology, participants, trends, market size and share
几款开源自动化测试框架优缺点对比你知道吗?
{1,2,3,2,5} duplicate checking problem
Fundamentals of digital circuits (III) encoder and decoder
ucore lab5用户进程管理 实验报告
Global and Chinese market of RF shielding room 2022-2028: Research Report on technology, participants, trends, market size and share