📜  Python|产量关键字

📅  最后修改于: 2022-05-13 01:55:13.457000             🧑  作者: Mango

Python|产量关键字

Yield是Python中的一个关键字,用于从函数返回而不破坏其局部变量的状态,并且在调用该函数时,从最后一个 yield 语句开始执行。任何包含 yield 关键字的函数都称为生成器。因此,yield 是生成器的组成部分。 Python中的 yield 关键字鲜为人知,但它具有人们可以想到的更大的实用性。

代码 #1:演示产量工作

Python3
# Python3 code to demonstrate
# yield keyword
 
# generator to print even numbers
def print_even(test_list) :
    for i in test_list:
        if i % 2 == 0:
            yield i
 
# initializing list
test_list = [1, 4, 5, 6, 7]
 
# printing initial list
print ("The original list is : " +  str(test_list))
 
# printing even numbers
print ("The even numbers in list are : ", end = " ")
for j in print_even(test_list):
    print (j, end = " ")


Python3
# A Python program to generate squares from 1
# to 100 using yield and therefore generator
 
# An infinite generator function that prints
# next square number. It starts with 1
def nextSquare():
    i = 1
 
    # An Infinite loop to generate squares
    while True:
        yield i*i                
        i += 1 # Next execution resumes
                # from this point    
 
# Driver code
for num in nextSquare():
    if num > 100:
        break   
    print(num)


Python3
# Python3 code to demonstrate yield keyword
 
# Checking number of occurrence of
# geeks in string
 
# generator to print even numbers
def print_even(test_string) :
    for i in test_string:
        if i == "geeks":
            yield i
 
# initializing string
test_string = " The are many geeks around you, \
              geeks are known for teaching other geeks"
 
# printing even numbers using yield
count = 0
print ("The number of geeks in string is : ", end = "" )
test_string = test_string.split()
 
for j in print_even(test_string):
    count = count + 1
 
print (count)


输出 :

The original list is : [1, 4, 5, 6, 7]
The even numbers in list are :  4 6 


代码#2:

Python3

# A Python program to generate squares from 1
# to 100 using yield and therefore generator
 
# An infinite generator function that prints
# next square number. It starts with 1
def nextSquare():
    i = 1
 
    # An Infinite loop to generate squares
    while True:
        yield i*i                
        i += 1 # Next execution resumes
                # from this point    
 
# Driver code
for num in nextSquare():
    if num > 100:
        break   
    print(num)

输出:

1
4
9
16
25
36
49
64
81
100

产量优势:

  • 由于它存储局部变量状态,因此控制了内存分配的开销。
  • 由于保留了旧状态,因此流程不会从头开始,因此可以节省时间。

产量的缺点:

  • 有时,如果函数调用处理不当,yield 的使用就会出错。
  • 时间和内存优化以代码复杂性为代价,因此有时难以理解其背后的逻辑。

实际应用:
可能的实际应用是,在处理最后一批数据并从中搜索细节时,可以使用yield,因为我们不需要从头开始再次查找,因此可以节省时间。根据用例的不同,yield 可能有很多应用。

Python3

# Python3 code to demonstrate yield keyword
 
# Checking number of occurrence of
# geeks in string
 
# generator to print even numbers
def print_even(test_string) :
    for i in test_string:
        if i == "geeks":
            yield i
 
# initializing string
test_string = " The are many geeks around you, \
              geeks are known for teaching other geeks"
 
# printing even numbers using yield
count = 0
print ("The number of geeks in string is : ", end = "" )
test_string = test_string.split()
 
for j in print_even(test_string):
    count = count + 1
 
print (count)

输出 :

The number of geeks in string is : 3