
A function which returns a generator iterator. It looks like a normal function except that it contains yield expressions for producing a series of values usable in a for-loop or that can be retrieved one at a time with the next() function.
generator是一个使用yield关键字生成一系列数据的函数,可以通过for或者next()遍历其所有值。generator只有当使用时才会去尝试生成数据。
在函数中使用yield循环输出数据
def generator_function():
for i in range(10):
yield i
for item in generator_function():
print(item)
# Output: 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9或者,使用generator推导式(更list相似,使用()替代[])
csv_gen = (row for row in open(file_name))生成无限大数据,这时候使用list存放数据可能会出现内存不足甚至系统崩溃的情况。使用generator定义生成的方式,可以一次只取一个值的方式不断的生成数据,内存占用少。
Generators are best for calculating large sets of results (particularly calculations involving loops themselves) where you don’t want to allocate the memory for all results at the same time. Many Standard Library functions that return
listsin Python 2 have been modified to returngeneratorsin Python 3 becausegeneratorsrequire fewer resources.
例如不用generator读取大文件,一个可能出现的异常是MemoryError
def csv_reader(file_name):
file = open(file_name)
result = file.read().split("\n")
return result
Traceback (most recent call last):
File "ex1_naive.py", line 22, in <module>
main()
File "ex1_naive.py", line 13, in main
csv_gen = csv_reader("file.txt")
File "ex1_naive.py", line 6, in csv_reader
result = file.read().split("\n")
MemoryError这时候,使用generator就能解决这个问题
def csv_reader(file_name):
for row in open(file_name, "r"):
yield row生成斐波拉切数列
# generator version
def fibon(n):
a = b = 1
for i in range(n):
yield a
a, b = b, a + b生成一个无限序列
def infinite_sequence():
num = 0
while True:
yield num
num += 1创建Data Pipelines
file_name = "techcrunch.csv"
lines = (line for line in open(file_name))
list_line = (s.rstrip()split(",") for s in lines)
cols = next(list_line)
company_dicts = (dict(zip(cols, data)) for data in list_line)
funding = (
int(company_dict["raisedAmt"])
for company_dict in company_dicts
if company_dict["round"] == "a"
)
total_series_a = sum(funding)
print(f"Total series A fundraising: ${total_series_a}")原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。